我尝试了以下几种方法:
myfile.txt有以下内容,我想用Python语言中的正则表达式提取每个“abc start”和“abc end”之间的数据。感谢你的帮助abc start 1 2 3 4 abc end 5 6 7 abc start 8 9 10 abc end
期望输出为%1%2%3%4%8%9 10
发布于 2018-12-17 04:30:54
import re
with open('myfile.txt') as f:
txt = f.read()
strings = re.findall('abc start\n(.+?)\nabc end', txt, re.DOTALL)
# to transform to your output..
result = []
for s in strings:
result += s.split('\n')
print(result)
#['1', '2', '3', '4', '8', '9', '10']
发布于 2018-12-17 05:08:09
使用正则表达式
import re
string = ''
with open('file.txt','r') as f:
for i in f.readlines():
string +=i.strip()+' '
f.close()
exp = re.compile(r'abc start(.+?)abc end')
result = [[int(j) for j in list(i.strip().split())] for i in exp.findall(string)]
print(result)
# [[1, 2, 3, 4], [8, 9, 10]]
https://stackoverflow.com/questions/53806112
复制相似问题