I have a filename with thousands of lines of data in it.
I am reading in the filename and editing it.
The following tag is about ~900 lines in or more (it varies per file):
I need to remove that line and everything before it in several files.
so I need to the code to search for that tag and delete it and everything above it
it will not always be 900 lines down, it will vary; however, the tag will always be the same.
I already have the code to read in the lines and write to a file. I just need the logic behind finding that line and removing it and everything before it.
I tried reading the file in line by line and then writing to a new file once it hits on that string, but the logic is incorrect:
readFile = open(firstFile)
lines = readFile.readlines()
readFile.close()
w = open('test','w')
for item in lines:
if (item == ""):
w.writelines(item)
w.close()
In addition, the exact string will not be the same in each file. The value "test" will be different. I perhaps need to check for the tag name ""
Any help will be much appreciated.
解决方案
You can use a flag like tag_found to check when lines should be written to the output. You initially set the flag to False, and then change it to True once you've found the right tag. When the flag is True, you copy the line to the output file.
TAG = ''
tag_found = False
with open('tag_input.txt') as in_file:
with open('tag_output.txt', 'w') as out_file:
for line in in_file:
if not tag_found:
if line.strip() == TAG:
tag_found = True
else:
out_file.write(line)
PS: The with open(filename) as in_file: syntax is using what Python calls a "context manager"- see here for an overview. The short explanation of them is that they automatically take care of closing the file safely for you when the with: block is finished, so you don't have to remember to put in my_file.close() statements.