Welcome, guest | Sign In | My Account | Store | Cart

This is almost too trivial, but perhaps it will be useful to perl fugitives searching for a pythonic way to remove line endings from strings.

The answer: use rstrip()

Python, 7 lines
1
2
3
4
5
6
7
# single-liners using rstrip()

strvar = strvar.rstrip()

strlist = [l.strip for l in strlist]

filelines = [l.rstrip() for l in open(filename)]

1 comment

Jay Graves 17 years, 11 months ago  # | flag

Not quite the same as chomp. 'chomp' removes the newline only. An equivalent call would be:

strvar = strvar.rstrip('\n')

A bare 'rstrip' removes all whitespace from the end.

Created by James G. Sack on Mon, 1 May 2006 (PSF)
Python recipes (4591)
James G. Sack's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks