read lines without the line break character at the end?

W

Wai Yip Tung

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
 
M

Michael Hudson

Wai Yip Tung said:
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
 
P

Paul McGuire

Wai Yip Tung said:
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
 
W

Wai Yip Tung

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 said:
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
 
S

Stan Cook

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
 
A

Andrew Dalke

Stan said:
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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top