tags:

views:

35969

answers:

6

This is one of my most common questions when I am coding Python (I was fed Perl as a baby and am forever trying to get rid of that affliction) and I wanted to put it out there on stack overflow so that next time I search for 'chomp python' on google, I get a useful answer.

+45  A: 

Try the rstrip method.

>>> 'test string\n'.rstrip()
'test string'

Note that Python's rstrip method strips all kinds of whitespace by default, not just newlines as Perl does with chomp. To strip only newlines:

>>> 'test string \n'.rstrip('\n')
'test string '

There is also the lstrip and strip methods.

>>> s = " \n  abc   def   "
>>> s.strip()
'abc   def'
>>> s.rstrip()
' \n  abc   def'
>>> s.lstrip()
'abc   def   '
>>>
Rich Bradshaw
Not that this is community wiki, so that I don't gain any reputation from it.
Rich Bradshaw
Added a few more examples
MizardX
I'm not a Python person so I don't have the answer to this, but Perl's chomp() actually removes the input record separator from the end. That's a newline on Unixy things, but may be different (e.g. Windows) and it's mutable. Is there a way to remove that value only once from the end of a string?
brian d foy
brian d foy: Python doesn't have an input record separator like awk and Perl have.
Peter Hosey
Is \n sufficient?>>> "test string\r\n".rstrip("\n")'test string\r'
Andrew Grimm
+7  A: 

The canonical way to strip end-of-line (EOL) characters is to use the string rstrip() method removing any trailing \r or \n. Here are examples for Mac, Windows, and Unix EOL characters.

>>> 'Mac EOL\r'.rstrip('\r\n')
'Mac EOL'
>>> 'Windows EOL\r\n'.rstrip('\r\n')
'Windows EOL'
>>> 'Unix EOL\n'.rstrip('\r\n')
'Unix EOL'

Using '\r\n' as the parameter to rstrip means that it will strip out any trailing combination of '\r' or '\n'. That's why it works in all three cases above.

This nuance matters in rare cases. For example, I once had to process a text file which contained an HL7 message. The HL7 standard requires a trailing '\r' as its EOL character. The Windows machine on which I was using this message had appended its own '\r\n' EOL character. Therefore, the end of each line looked like '\r\r\n'. Using rstrip('\r\n') would have taken off the entire '\r\r\n' which is not what I wanted. In that case, I simply sliced off the last two characters instead.

Mike
Note that modern Mac OS X apps use \n. Only old Carbon apps originally written for Mac OS use \r.
Peter Hosey
Thanks for the clarification. Of course, the rstrip('\r\n') still works in that case too.
Mike
+13  A: 

And I would say the "pythonic" way to get lines without trailing newline characters is splitlines().

>>> text = "line 1\nline 2\r\nline 3\nline 4"
>>> text.splitlines()
['line 1', 'line 2', 'line 3', 'line 4']
Ryan Ginstrom
+7  A: 

Note that rstrip doesn't act exactly like Perl's chomp() because it doesn't modify the string. That is, in Perl:

$x="a\n";

chomp $x

results in $x being "a".

but in Python:

x="a\n"

x.rstrip()

will mean that the value of x is still "a\n". You need to write x=x.rstrip() to get the equivalent behavior.

Also, strip() removes repeated characters, whereas chop/chomp only removes one newline
kostmo
+3  A: 

I don't program in Python, but I came across an FAQ at python.org advocating S.rstrip("\r\n") for python 2.2 or later.

Alternatively, switch to Ruby. It has chomp.

Andrew Grimm
It would be a pretty lame reason to switch language.
Tom Leys
The downvote may have been justified if I hadn't included the first sentence, but ... wow. Someone's touchy!
Andrew Grimm
+1 for showing the FAQ, even though you advocate another language: it's helpful for someone who does use this language.
Zachary Young
+4  A: 

I might use something like this:

import os
s = s.rstrip(os.linesep)

I think the problem with rstrip("\n") is that you'll probably want to make sure the line separator is portable. (some antiquated systems are rumored to use "\r\n") Hopefully os.linesep will contain the right characters.. the above works for me.

Jamie
"some antiquated systems" LOL! (we won't mention it out loud) :)
orange80