Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > strip() 2.4.4

Reply
Thread Tools

strip() 2.4.4

 
 
Nick
Guest
Posts: n/a
 
      06-21-2007
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

 
Reply With Quote
 
 
 
 
Stephen R Laniel
Guest
Posts: n/a
 
      06-21-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
 
Reply With Quote
 
 
 
 
Richie Hindle
Guest
Posts: n/a
 
      06-21-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
(E-Mail Removed)
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      06-21-2007
In article <(E-Mail Removed) m>,
Nick <(E-Mail Removed)> 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.
 
Reply With Quote
 
Stephen R Laniel
Guest
Posts: n/a
 
      06-21-2007
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
(E-Mail Removed)
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      06-21-2007
On 2007-06-21, Nick <(E-Mail Removed)> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-21-2007
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
 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      06-21-2007
On 21 Jun, 14:53, Neil Cerutti <(E-Mail Removed)> wrote:
> On 2007-06-21, Nick <(E-Mail Removed)> 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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments