By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

read lines without the line break character at the end?

P: n/a
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?

Thanks,

tung
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Wai Yip Tung" <tu********@yahoo.com> writes:
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?


No. line.rstrip('\n') is a shorter way of trimming the newline
character, though.

I guess adding

def without_newlines(thing):
for line in thing:
yield line.rstrip('\n')

to your snippet collection will do.

Cheers,
mwh

--
Famous remarks are very seldom quoted correctly.
-- Simeon Strunsky
Jul 18 '05 #2

P: n/a
"Wai Yip Tung" <tu********@yahoo.com> wrote in message
news:op**************@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
Jul 18 '05 #3

P: n/a
Thanks, I like the rstrip() syntax. Coming from Java background. I made
too many mistakes forgetting to strip the '\n' before using the string.
"Wai Yip Tung" <tu********@yahoo.com> writes:
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?


No. line.rstrip('\n') is a shorter way of trimming the newline
character, though.

I guess adding

def without_newlines(thing):
for line in thing:
yield line.rstrip('\n')

to your snippet collection will do.

Cheers,
mwh


Jul 18 '05 #4

P: n/a
try for line in fp:
whatever line[:-1] etc etc

Whatever you're doing with the line, the new line character will be gone.
Stan
"Wai Yip Tung" <tu********@yahoo.com> wrote in message
news:op**************@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?

Thanks,

tung

Jul 18 '05 #5

P: n/a
Stan Cook wrote:
try for line in fp:
whatever line[:-1] etc etc

Whatever you're doing with the line, the new line character will be gone.


Except that 1) you should open the file in "U"niversal
mode, and 2) this will chop the last character from the
last line in the file if the file doesn't end with
a newline.

Andrew
da***@dalkescientific.com

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.