一個字符串裏面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)