Nick wrote: > strip() isn't working as i expect, am i doing something wrong - > > Sample data in file in.txt: > > 'AF':'AFG':'004':'AFGHANISTAN':'Afghanistan' > 'AL':'ALB':'008':'ALBANIA':'Albania' > 'DZ':'DZA':'012':'ALGERIA':'Algeria' > 'AS':'ASM':'016':'AMERICAN SAMOA':'American Samoa' > > > Code: > > f1 = open('in.txt', 'r') > > for line in f1: > print line.rsplit(':')[4].strip("'"), > > Output: > > Afghanistan' > Albania' > Algeria' > 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: import csv for row in csv.reader(f1, delimiter=":", quotechar="'"): print row[-1] that should work, too. Peter -- http://mail.python.org/mailman/listinfo/python-list