Problem with string parsing

Discussion in 'Python' started by Mike Howard, Oct 5, 2004.

  1. Mike Howard

    Mike Howard Guest

    If I do this:
    string.lstrip('1_mature_dt=10-May-2002','1_mature_dt=')
    I get:
    '0-May-2002'
    Which I did not expect
    string.lstrip('1_mature_dt=20-May-2002','1_mature_dt=')
    I get
    '20-May-2002'
    Which I expected
    string.lstrip('1_mature_dt=01-Jun-2003','1_mature_dt=')
    I get
    '01-Jun-2003'
    Which I expected

    Is this a bug or am I doing something wrong?
     
    Mike Howard, Oct 5, 2004
    #1
    1. Advertising

  2. Mike Howard

    Remy Blank Guest

    Mike Howard wrote:
    > If I do this:
    > string.lstrip('1_mature_dt=10-May-2002','1_mature_dt=')
    > I get:
    > '0-May-2002'
    > Which I did not expect


    The second argument of lstrip() is not a string to remove from the
    beginning of a string but a set of characters to strip. In this case,
    there is a '1' in that set, so it gets stripped from the '10-May-2002'.

    > Is this a bug or am I doing something wrong?


    It is by design.

    -- Remy


    Remove underscore and suffix in reply address for a timely response.
     
    Remy Blank, Oct 5, 2004
    #2
    1. Advertising

  3. On 5 Oct 2004 07:38:09 -0700, Mike Howard <> wrote:
    > If I do this:
    > string.lstrip('1_mature_dt=10-May-2002','1_mature_dt=')
    > I get:
    > '0-May-2002'
    > Which I did not expect


    string.strip() and friends strips from the first argument *any* of the
    characters in the second argument:

    >>> string.strip('***###spam***###eggs***###','*#')

    'spam***###eggs'

    Make sense now?

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Oct 5, 2004
    #3
  4. Am Tue, 05 Oct 2004 07:38:09 -0700 schrieb Mike Howard:

    > If I do this:
    > string.lstrip('1_mature_dt=10-May-2002','1_mature_dt=')
    > I get:
    > '0-May-2002'
    > Which I did not expect


    Hi,

    from the doc:
    """If given and not None, chars must be a string; the characters in the
    string will be stripped from the beginning of the string this method is
    called on"""

    the second argument is a *list* of characters. Since "1" is
    in the list, if gets stripped from "10-May", too.

    You could use
    mystring='1_mature_dt=10-May-2002'
    mystart="1_mature_dt="
    if mystring.startswith(mystart):
    mystring=mystring[len(mystart):]

    BTW, you don't need the string-module. You can
    use mystring.strip().

    HTH,
    Thomas
     
    Thomas Guettler, Oct 5, 2004
    #4
  5. Mike Howard

    Russell Blau Guest

    "Mike Howard" <> wrote in message
    news:...
    > If I do this:
    > string.lstrip('1_mature_dt=10-May-2002','1_mature_dt=')


    why not '1_mature_dt=10-May-2002'.lstrip('1_mature_dt=') ?

    > I get:
    > '0-May-2002'
    > Which I did not expect


    You have been bitten by the obscure documentation of the .lstrip() method.
    The second argument is viewed as a collection of characters, not as a
    substring; so, since there is a "1" in the collection of characters to be
    stripped, the method removed the "1" at the beginning of the date. It stops
    when it gets to the first character *not* in the collection, which in your
    case is the "0".


    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.
     
    Russell Blau, Oct 5, 2004
    #5
  6. Mike Howard

    wes weston Guest

    Mike Howard wrote:
    > If I do this:
    > string.lstrip('1_mature_dt=10-May-2002','1_mature_dt=')
    > I get:
    > '0-May-2002'
    > Which I did not expect
    > string.lstrip('1_mature_dt=20-May-2002','1_mature_dt=')
    > I get
    > '20-May-2002'
    > Which I expected
    > string.lstrip('1_mature_dt=01-Jun-2003','1_mature_dt=')
    > I get
    > '01-Jun-2003'
    > Which I expected
    >
    > Is this a bug or am I doing something wrong?


    Mike,
    Note that after the appropriate if:

    >>> '1_mature_dt=10-May-2002'[len('1_mature_dt='):]

    '10-May-2002'

    wes
     
    wes weston, Oct 5, 2004
    #6
  7. wes weston wrote:
    > Mike,
    > Note that after the appropriate if:
    >
    > >>> '1_mature_dt=10-May-2002'[len('1_mature_dt='):]

    > '10-May-2002'


    Though I would guess that the following is closer to
    what was actually thought by the OP to be the behaviour of
    ..lstrip():

    def lstripsub(s, sub):
    if s.startswith(sub):
    return s[len(sub):]
    else:
    return s

    >>> s = '1_mature_dt=10-May-2002'
    >>> lstripsub(s, '1_mature_dt=')

    '10-May-2002'

    -Peter
     
    Peter L Hansen, Oct 6, 2004
    #7
  8. In article <>,
    Peter L Hansen <> wrote:

    > wes weston wrote:
    > > Mike,
    > > Note that after the appropriate if:
    > >
    > > >>> '1_mature_dt=10-May-2002'[len('1_mature_dt='):]

    > > '10-May-2002'

    >
    > Though I would guess that the following is closer to
    > what was actually thought by the OP to be the behaviour of
    > .lstrip():
    >
    > def lstripsub(s, sub):
    > if s.startswith(sub):
    > return s[len(sub):]
    > else:
    > return s
    >
    > >>> s = '1_mature_dt=10-May-2002'
    > >>> lstripsub(s, '1_mature_dt=')

    > '10-May-2002'


    Or, perhaps:

    def lstripsub(s, sub):
    while s.startswith(sub):
    s = s[len(sub):]

    return s

    You could argue it should strip ALL leading occurrences of the leader,
    to be consistent with the behaviour of str.lstrip().

    But that, I realize, is being somewhat pedantic, and I do not dispute
    that your solution is quite reasonable. :)

    -M

    --
    Michael J. Fromberger | Lecturer, Dept. of Computer Science
    http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
     
    Michael J. Fromberger, Oct 6, 2004
    #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. GIMME
    Replies:
    2
    Views:
    893
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    591
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    616
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    445
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    747
    John Levine
    Feb 2, 2012
Loading...

Share This Page