strip() 2.4.4

Neil Cerutti horpner at yahoo.com
Thu Jun 21 09:53:04 EDT 2007


On 2007-06-21, 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?

Most likely it's the newline at the end of each record that's
getting in your way.

You can double-strip it.

for line in f1:
    print line.strip().rsplit(':')[4].strip("'")

-- 
Neil Cerutti
The world is more like it is now than it ever has been before. --Dwight
Eisenhower



More information about the Python-list mailing list