Reputation: 13
I'm trying to process text files in python. The file structure looks somewhat like this:
info to process
info to process
START
...
END
info to process
START
...
END
I need to process the file line by line (i'm using simple "for line in file" for that) but i also need to remove anything that's between START and END.
The most similar problem i found here would be this one here The problem is that:
I thought about adding variable, setting it to true when it meets START and to false when it meets END and save output based on this variable, but this seems very not-python-like way to implement this.
I expect the end file to look like this
Processed info
Processed info
Processed info
Upvotes: 1
Views: 69
Reputation: 372
Personally, I don't understand what you mean by characterizing your proposed solution as "very not-python-like."
I implemented your suggestion as follows and got the outcome you expected:
with open('test.txt', 'r') as f_orig, open('test2.txt', 'w') as f_new:
for line in f_orig:
if line[:5] == 'START':
skipping = True
if not skipping:
f_new.write(line)
if line[:3] == 'END':
skipping = False
Upvotes: 1