strip() 2.4.4
Richie Hindle
richie at entrian.com
Thu Jun 21 09:32:15 EDT 2007
[Nick]
> Why is there a apostrophe still at the end?
[Stephen]
> Is it possible that you actually have whitespace at the end
> of the line?
It's the newline - reading lines from a file doesn't remove the newlines:
from cStringIO import StringIO
DATA = """\
'AF':'AFG':'004':'AFGHANISTAN':'Afghanistan'
'AL':'ALB':'008':'ALBANIA':'Albania'
'DZ':'DZA':'012':'ALGERIA':'Algeria'
'AS':'ASM':'016':'AMERICAN SAMOA':'American Samoa'
"""
f1 = StringIO(DATA)
for line in f1:
print repr(line.rsplit(':')[4].strip("'")) # repr shows the error
# This prints:
#
# "Afghanistan'\n"
# "Albania'\n"
# "Algeria'\n"
# "American Samoa'\n"
#
# Do this instead:
f1.seek(0)
for line in f1:
print line.strip().rsplit(':')[4].strip("'")
# This prints:
#
# Afghanistan
# Albania
# Algeria
# American Samoa
--
Richie Hindle
richie at entrian.com
More information about the Python-list
mailing list