I have a very big file that I need to parse. I don't need any of the lines up to '&'. I just need the information after the '&' in the file. How do I delete the lines before the '&'? This is what I have so far:
import re
original_file = 'file.rpt'
file_copy = 'file_copy.rpt'
with open(original_file, 'r') as rf:
with open(file_copy, 'r+') as wf:
for line in rf:
#if statement to write after the '&' has been encountered?
wf.write(line)
Input file:
sample text1
sample text2
sample text3
sample text4
&sample text5
sample text6
expected output file:
&sample text5
sample text6
In the rpt file, it has 6 lines, lines 1-4 are information that isn't needed. I want to delete lines 1-4, so I can focus on lines 5 and 6.
A better and safer way would be to create a new file with smaller contents so that you can check the contents before deleting the old file. So my suggestion would look like this:
This code will omit all the lines up to and excluding the line containing the
&You can also analyze the line with
&symbol:The above will write also all the contents after
&but in the same line omitting anything before&in the same lineEDIT
Also check if your opening the second file in a correct mode maybe you should use
'w'to truncate file first'r+'will append to the contents of the file and I am not sure this is what you want