我正在尝试以下操作:
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
答案 0 :(得分:1)
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']
答案 1 :(得分:1)
使用正则表达式
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]]