problem parsing lines in a file

Matimus mccredie at gmail.com
Tue Dec 11 13:21:57 EST 2007


> This result is a copy of "ProblemList" without any changes made.  What
> am I doing wrong?  Thanks for any help.

rstrip doesn't work the way you think it does

>>> help(str.rstrip)
Help on method_descriptor:

rstrip(...)
    S.rstrip([chars]) -> string or unicode

    Return a copy of the string S with trailing whitespace removed.
    If chars is given and not None, remove characters in chars
instead.
    If chars is unicode, S will be converted to unicode before
stripping

>>> 'abcdef'.rstrip('e')
'abcdef'
>>> 'abcdef'.rstrip('ef')
'abcd'
>>> 'abcdef'.rstrip('efc')
'abcd'
>>> 'abcdef'.rstrip('efcd')
'ab'
>>> '20,3,"Bubonic plague",11/11/2003 0:00:00\n'.rstrip('0:00:00')
'20,3,"Bubonic plague",11/11/2003 0:00:00\n'
>>> '20,3,"Bubonic plague",11/11/2003 0:00:00\n'.rstrip('0:00:00\n')
'20,3,"Bubonic plague",11/11/2003 '
>>> '20,3,"Bubonic plague",11/11/2003 0:00:00\n'.rstrip('0:\n')
'20,3,"Bubonic plague",11/11/2003 '

You probably just want to use slicing though:

>>> '20,3,"Bubonic plague",11/11/2003 0:00:00\n'[:-9]
'20,3,"Bubonic plague",11/11/2003'

But don't forget to re-attach a newline before writing out. That goes
for the first method also.

Matt



More information about the Python-list mailing list