strip() 2.4.4

Discussion in 'Python' started by Nick, Jun 21, 2007.

  1. Nick

    Nick Guest

    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
     
    Nick, Jun 21, 2007
    #1
    1. Advertising

  2. 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

    Cell: +(617) 308-5571
    http://laniels.org/
    PGP key: http://laniels.org/slaniel.key
     
    Stephen R Laniel, Jun 21, 2007
    #2
    1. Advertising

  3. [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
     
    Richie Hindle, Jun 21, 2007
    #3
  4. Nick

    Roy Smith Guest

    In article <>,
    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?


    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.
     
    Roy Smith, Jun 21, 2007
    #4
  5. 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

    Cell: +(617) 308-5571
    http://laniels.org/
    PGP key: http://laniels.org/slaniel.key
     
    Stephen R Laniel, Jun 21, 2007
    #5
  6. Nick

    Neil Cerutti Guest

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


    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
     
    Neil Cerutti, Jun 21, 2007
    #6
  7. Nick

    Peter Otten Guest

    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
     
    Peter Otten, Jun 21, 2007
    #7
  8. Nick

    Nick Guest

    On 21 Jun, 14:53, Neil Cerutti <> wrote:
    > On 2007-06-21, 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?

    >
    > 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.
     
    Nick, Jun 21, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Evanchik
    Replies:
    3
    Views:
    577
    Yan-Hong Huang[MSFT]
    Jul 30, 2003
  2. Michal A. Valasek

    How to strip HTML markup from string?

    Michal A. Valasek, Aug 9, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    5,276
    Yan-Hong Huang[MSFT]
    Aug 12, 2003
  3. vMike

    Strip space before page rendering

    vMike, Oct 30, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    4,229
    Brian W
    Oct 30, 2003
  4. Aquila
    Replies:
    35
    Views:
    454
    Mathieu Bouchard
    Mar 31, 2005
  5. yelipolok
    Replies:
    4
    Views:
    263
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page