[Tutor] Iterating Lines in File and Export Results
Peter Otten
__peter__ at web.de
Fri Oct 3 00:08:13 CEST 2014
John Doe wrote:
> Hello List,
> I am in need of your assistance. I have a text file with random words
> in it. I want to write all the lines to a new file. Additionally, I am
> using Python 2.7 on Ubuntu 12.04:
>
> Here is my code:
>
> def loop_extract():
> with open('words.txt', 'r') as f:
> for lines in f:
The name `lines` is misleading, you are reading one line at a time.
> #print lines (I confirmed that each line is successfully
> #printed)
> with open('export.txt', 'w') as outf:
> outf.write(lines)
> #outf.write(lines)
> #outf.write('{}\n'.format(lines))
> #outf.write('{}\n'.format(line for line in lines))
>
>
> For some reason, the second file only contains the last line from the
> original file -- I have tried multiple variations (.read, .readlines,
> .writelines, other examples preceded by comment from above and many
> more) and tried to use the module, fileinput, but I still get the same
> results.
Every time the line
> with open('export.txt', 'w') as outf:
is executed the file "export.txt" is truncated:
https://docs.python.org/dev/library/functions.html#open
To avoid the loss of data open the file once, outside the loop:
with open("words.txt") as infile, open("export.txt", "w") as outfile:
for line in infile:
outfile.write(line)
> I do understand there is another way to copy the file over, but to
> provide additional background information on my purpose -- I want to
> read a file and save successful regex matches to a file; exporting
> specific data. There doesn't appear to be anything wrong with my
> expression as it prints the expected results without failure. I then
> decided to just write the export function by itself in its basic form,
> per the code above, which the same behavior occurred;
That is a good approach! Reduce the code until only the source of the
problem is left.
> only copying the
> last line. I've googled for hours and, unfortunately, at loss.
I do that too, but not "for hours" ;)
> I want to read a file and save successful regex matches to a file;
> exporting specific data.
An experienced user of Python might approach this scenario with a generator:
def process_lines(infile):
for line in infile:
line = process(line) # your line processing
if meets_condition(line): # your filter condition
yield line
with open("words.txt") as infile:
with open("export.txt", "w") as outfile:
outfile.writelines(
process_lines(infile))
More information about the Tutor
mailing list