Perl chop equivalent in Python?

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Wed May 31 14:53:51 EDT 2000


pem at my-deja.com wrote in comp.lang.python:
> Is there an equivalent command to chop (the perl command) in Python?  If
> not, is there some easy way to use existing commands to do this simple
> task?  I need to do it frequently and would like to have a readily
> available command that does not require much programming.  Thanks in
> advance.

string.rstrip() removes all trailing whitespace.

If you only want to delete trailing newlines when they're there, just use
something like

if line[-1] == '\n': line = line[:-1]

Or, if you read a file using "lines = file.readlines()", you could do "lines
= split(file.read(),'\n')" instead, so that the lines don't have newlines.

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
  8:53pm  up 86 days,  9:06,  5 users,  load average: 0.00, 0.01, 0.03



More information about the Python-list mailing list