一个字符串里面aAd123sdacD12dad2,而后遇到数字取第一个,后面的数字用来分隔,结果是这样
【aAd1,sdacD1,dad2】git
import string
ss = 'aaa123bbb321cc456'
flag = 1
str_temp = ''
a = []
for i in ss:
if i.isalpha():
str_temp += i
flag = 1
elif i.isdigit():
if flag == 1:
str_temp += i
a.append(str_temp)
flag = 0
str_temp = ''
print(a)
f.close()
['aaa1', 'bbb3', 'cc4']
s = 'aaa123bb234cc345' res = [] t = '' for i in s: t += i if i.isdigit(): if not t.isdigit(): res.append(t) t = '' print(res)
import re ss = '22aaa123bbb321cc456' temp = re.findall(pattern='\D+\d', string=ss) print(temp)
查找文件中以print开头的行的内容及行号 import re count = 1 d = {} f = open("C:/Users/asus/Desktop/1.py",'r') tmp = f.readline() if tmp == '': d[count] = tmp # pattern = re.compile(r"^print") while tmp: str = re.findall(pattern='^print', string=tmp, flags=re.I) if len(str) != 0: # sb = "%d:%s" % (count, tmp) d[count] = tmp count += 1 tmp = f.readline() print(d)