strip() 2.4.4
Roy Smith
roy at panix.com
Thu Jun 21 09:44:41 EDT 2007
In article <1182432181.050589.5800 at n2g2000hse.googlegroups.com>,
Nick <nickjbyrne at gmail.com> 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?
No clue, I can't reproduce it, but here's some ideas to try.
1) It helps to give more information. Exactly what version of python are
you using? Cut-and-paste what python prints out when you start it up
interactively, i.e.:
Python 2.4 (#1, Jan 17 2005, 14:59:14)
[GCC 3.3.3 (NetBSD nb3 20040520)] on netbsd2
More than likely, just saying "2.4" would tell people all they need to
know, but it never hurts to give more info.
2) Try to isolate what's happening. Is the trailing quote really in the
string, or is print adding it? Do something like:
temp = line.rsplit(':')[4].strip("'")
print repr (temp[0])
and see what happens.
3) Are you sure the argument you're giving to strip() is the same character
that's in the file? Is it possible the file has non-ascii characters, such
as "smart quotes"? Try printing ord(temp[0]) and ord(temp("'")) and see if
they give you the same value.
More information about the Python-list
mailing list