Splitting text into lines

Steve D'Aprano steve+python at pearwood.info
Tue Dec 13 19:05:46 EST 2016


On Wed, 14 Dec 2016 03:45 am, George Trojan - NOAA Federal wrote:

> I have files containing ASCII text with line s separated by '\r\r\n'.
> Example:
> 
> $ od -c FTAK31_PANC_131140.1481629265635
> 0000000   F   T   A   K   3   1       P   A   N   C       1   3   1   1
> 0000020   4   0  \r  \r  \n   T   A   F   A   B   E  \r  \r  \n   T   A
[...] 
> 0000300  \r  \r  \n
> 0000303
> 
> What is the proper way of getting a list of lines?

Do you have any objection to post-processing the list of lines?

lines = open(filename).readlines()
# remove leading and trailing whitespace, including newline characters
lines = [line.strip() for line in lines]
# get rid of empty strings
lines = [line for line in lines if line]

If you prefer, that last line of code can be written as either:

lines = filter(None, lines)  # Python 2
lines = list(filter(None, lines))  # Python 3


Personally, this would be my preferred technique, as I (nearly) always end
up doing a strip() on data I read from text files, so the extra call to
filter is no big deal.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.



More information about the Python-list mailing list