strip() 2.4.4

N

Nick

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?

Thanks in advance.
Nick
 
R

Richie Hindle

[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
 
R

Roy Smith

Nick said:
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.
 
N

Neil Cerutti

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("'")
 
P

Peter Otten

Nick said:
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
 
N

Nick

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'

f1 = open('in.txt', 'r')
for line in f1:
print line.rsplit(':')[4].strip("'"),

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("'")

Thank you all very much for your input, the above solved the problem
as most of you had already pointed out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top