problem parsing lines in a file

Diez B. Roggisch deets at nospam.web.de
Tue Dec 11 12:35:00 EST 2007


barronmo wrote:

> I'm having difficulty getting the following code to work.  All I want
> to do is remove the '0:00:00' from the end of each line.  Here is part
> of the original file:
> 
> 3,3,"Dyspepsia NOS",9/12/2003 0:00:00
> 4,3,"OA of lower leg",9/12/2003 0:00:00
> 5,4,"Cholera NOS",9/12/2003 0:00:00
> 6,4,"Open wound of ear NEC*",9/12/2003 0:00:00
> 7,4,"Migraine with aura",9/12/2003 0:00:00
> 8,6,"HTN [Hypertension]",10/15/2003 0:00:00
> 10,3,"Imerslund syndrome",10/27/2003 0:00:00
> 12,4,"Juvenile neurosyphilis",11/4/2003 0:00:00
> 13,4,"Benign paroxysmal positional nystagmus",11/4/2003 0:00:00
> 14,3,"Salmonella infection, unspecified",11/7/2003 0:00:00
> 20,3,"Bubonic plague",11/11/2003 0:00:00
> 
> output = open('my/path/ProblemListFixed.txt', 'w')
> for line in open('my/path/ProblemList.txt', 'r'):
>      newline = line.rstrip('0:00:00')
>      output.write(newline)
> output.close()
> 
> 
> This result is a copy of "ProblemList" without any changes made.  What
> am I doing wrong?  Thanks for any help.

It works kind of for me - but it's actually a bit more than you want. Take a 
close look on what rstrip _really_ does. Small hint:

print "foobar".rstrip("rab")

Diez



More information about the Python-list mailing list