用字符串查找下一个直接行(找到第一个字符串后)

时间:2013-10-09 01:03:04

标签: python

我正在尝试从大型文本文件中删除一些信息并遇到一些问题。

此文件中有几个项目将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)

1 个答案:

答案 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