473,380 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,380 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 2173
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
2
by: Gustavo Randich | last post by:
Hello, (Using DB2/LINUX 8.2.0) After compiling the STRIP migration UDF (found at http://www-106.ibm.com/developerworks/db2/library/samples/db2/0205udfs/), if I try to execute: values...
5
by: dan.j.weber | last post by:
I'm using Python 2.3.5 and when I type the following in the interactive prompt I see that strip() is not working as advertised: >>>s = 'p p:p' >>>s.strip(' :') 'p p:p' Is this just me or...
6
by: rtilley | last post by:
s = ' qazwsx ' # How are these different? print s.strip() print str.strip(s) Do string objects all have the attribute strip()? If so, why is str.strip() needed? Really, I'm just curious......
4
by: Steve | last post by:
Hi, I'm a complete PHP n00b slowly finding my way around I'm using the following function that I found on php.net to strip out html and return only the text. It works well except for when you...
6
by: eight02645999 | last post by:
hi can someone explain strip() for these : 'example' when i did this: 'abcd,words.words'
3
by: Colin J. Williams | last post by:
The Library Reference has strip( ) Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed....
6
by: Christoph Zwerschke | last post by:
In Python programs, you will quite frequently find code like the following for removing a certain prefix from a string: if url.startswith('http://'): url = url Similarly for stripping...
4
by: Poppy | last post by:
I'm using versions 2.5.2 and 2.5.1 of python and have encountered a potential bug. Not sure if I'm misunderstanding the usage of the strip function but here's my example. var = "detail.xml"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.