我正在尝试从大型文本文件中删除一些信息并遇到一些问题。
此文件中有几个项目将start_needle作为ID,但它们无序。 end_needle是表示项目结尾的字符串。我能够得到起点,但是我如何拉出end_needle的下一个直接实例出现的那一行?
基本上,“在start_needle之后找到end_needle的下一个实例”
start_needle = '725160001'
end_needle = '* * END ITEM * *'
filename = 'LAS3300Combined.txt'
target = open('file.txt', 'w')
start_list = []
with open(filename) as myFile:
for num, line in enumerate(myFile, 1):
if start_needle in line:
start_list.append(num)
答案 0 :(得分:0)
开始和结束时切换布尔标志:
start_list = []
end_list = []
started = False
with open(filename) as myFile:
for num, line in enumerate(myFile, 1):
if not started and start_needle in line:
start_list.append(num)
started = True
if started and line.endswith(end_needle):
end_list.append(num)
started = False