copy section of text file - python

时间:2018-03-23 00:27:39

标签: python-3.x text copy

From a previous post (see below): How could I modify this to copy out multiple sections of a text file each beginning and ending with "start and "end" strings? This code only will find one instance of "start" and "end" if not mistaken (per a previous post).

with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
copy = False
for line in infile:
    if line.strip() == "Start":
        outfile.write(line) # add this
        copy = True
    elif line.strip() == "End":
        outfile.write(line) # add this
        copy = False
    elif copy:
        outfile.write(line)

0 个答案:

没有答案