__peter__ at web.de
Thu Jun 21 15:59:12 CEST 2007
> strip() isn't working as i expect, am i doing something wrong -
> Sample data in file in.txt:
> 'AS':'ASM':'016':'AMERICAN SAMOA':'American Samoa'
> f1 = open('in.txt', 'r')
> for line in f1:
> print line.rsplit(':').strip("'"),
> American Samoa'
> Why is there a apostrophe still at the end?
As others have already guessed, the problem is trailing whitespace, namely
the newline that you should have stripped
for line in f1:
line = line.rstrip("\n")
print line.rsplit(":", 1)[-1].strip("'")
instead of suppressing it with the trailing comma in the print statement.
Here is another approach that might work:
for row in csv.reader(f1, delimiter=":", quotechar="'"):
that should work, too.
More information about the Python-list