471,616 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

strip() 2.4.4

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

Jun 21 '07 #1
7 2064
On Thu, Jun 21, 2007 at 06:23:01AM -0700, Nick wrote:
Why is there a apostrophe still at the end?
Is it possible that you actually have whitespace at the end
of the line? So then strip() is looking for an apostrophe at
the end of the line, not finding it, and therefore not
stripping it?

--
Stephen R. Laniel
st***@laniels.org
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
Jun 21 '07 #2
In article <11********************@n2g2000hse.googlegroups.co m>,
Nick <ni********@gmail.comwrote:
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.
Jun 21 '07 #3
On Thu, Jun 21, 2007 at 01:42:03PM +0000, linuxprog wrote:
that should work for you ?
I reproduced the original poster's problem by adding one
extra space after the final "'" on each line. I'd vote that
that's the problem.

--
Stephen R. Laniel
st***@laniels.org
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
Jun 21 '07 #4
On 2007-06-21, Nick <ni********@gmail.comwrote:
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
Jun 21 '07 #5
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
Jun 21 '07 #6
On 21 Jun, 14:53, Neil Cerutti <horp...@yahoo.comwrote:
On 2007-06-21, Nick <nickjby...@gmail.comwrote:
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
Thank you all very much for your input, the above solved the problem
as most of you had already pointed out.

Jun 21 '07 #7

[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
ri****@entrian.com
Jun 21 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gustavo Randich | last post: by
5 posts views Thread by dan.j.weber | last post: by
6 posts views Thread by rtilley | last post: by
4 posts views Thread by Steve | last post: by
6 posts views Thread by eight02645999 | last post: by
3 posts views Thread by Colin J. Williams | last post: by
6 posts views Thread by Christoph Zwerschke | last post: by
4 posts views Thread by Poppy | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.