在python中统计两个字符串从首字符开始最大连续相同的字符数,函数如下: def get_num(s1, s2): num = 0 len_s1 = len(s1) list_s1 = [] for i in range(len_s1): two_s1 = s1[0:i+1] list_s1.append(two_s1) for i in list_s1: if s2.startswith(i) and len(i) > num: num = len(i) return num
0.2 2016.09.26 11:28* 字数 216 阅读 8053评论 2喜欢 5 最近一段时间的学习中发现,Python基本和中文字符杠上了.如果能把各种编码问题解决了,基本上也算对Python比较熟悉了. For UTF-8 encoding, Excel requires BOM (byte order mark) codepoint written at the start of the file or it will assume ANSI encoding, which is
问题背景:当我们爬取网页信息时,对于一些标签的提取是没有意义的,所以需要提取标签中间的信息. 解决办法:用到了re包下的函数 方法1:用到了research()方法和group()方法 方法2:用到了findall()方法 具体实现: import re # 匹配两个字符中间的所有字符 a = '<p>life is short, i use python<a/>i love it<p>' r = re.search('<p>(.*)<a/>(.
第二题:计算字符串中所有数字的和1.字符串中只有小写字母和数字2.数字可能连续,也可能不连续3.连续数字要当做一个数处s='1234adg3g11's1 = "" for i in s : if i.isdigit(): s1=s1+i else: s1=s1+" " lt = s1.split(" ") m= 0 for a in lt : if a.isdigit(): m=m+int(a) print(m) *解决思想:把字符串中得数字调出
python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: if not ('\u4e00' <= c <= '\u9fa5'): return False return True
# -*- coding:utf-8 -*- import sys,os txta = open('a.txt','r') str = '' for line in txta: str += line.strip().decode('utf-8') txta.close() for word in str: print word.encode('utf-8') 直接输出,是会乱码的,得先解码,再编码. 参考网址:http://blog.csdn.net/devil_2009/article/de