[Tutor] Omitting lines matching a list of strings from a file
galaxywatcher at gmail.com
galaxywatcher at gmail.com
Sun Feb 28 13:52:42 CET 2010
>> One formatting detail: there is a blank line after each line
>> printed, how do I ged rid of the extra blank lines?
> lines = [line.strip() for line in infile if line[146:148] not in
> omit_states]
> print '\n'.join(lines)
This approach stripped leading blank spaces introducing errors into my
fixed width file.
> or alternatively
>
> lines = [line for line in infile if line[146:148] not in omit_states]
> print ''.join(lines)
This works beautifully leaving leading blank spaces intact. Thanks.
> Just remember that doing a list comprehension like that on a large
> file will drastically reduce the speed of your application as well
> as introduce memory bloat.
Processing a file with well over 1 million records worked very
quickly, several seconds. Did not notice excessive memory bloat. I do
have 2 gigs of ram on my Macbook Pro however.
More information about the Tutor
mailing list