read lines without the line break character at the end?
Paul McGuire
ptmcg at austin.rr._bogus_.com
Thu Sep 2 12:44:21 EDT 2004
"Wai Yip Tung" <tungwaiyip at yahoo.com> wrote in message
news:opsdpjkwte433nmu at news.cisco.com...
> When I do
>
> for line in fp:
>
> the line string usually has a '\n' at the end. In many cases I don't want
> the line break character. I can trim it using
>
> if line.endswith('\n'): line = line[:-1]
>
> Given I do it so frequently I wonder if there is some builtin way to say I
> don't wnt the line break character?
>
If you have a lot of lines, you will be better off testing the value of
line[-1] instead of using endswith(). Here's some timeit results:
line.endswith('\n')
0.124841844424
line[-1] == '\n'
0.057334940614
-- Paul
More information about the Python-list
mailing list