string.lstrip stripping too much?

Discussion in 'Python' started by joram gemma, May 15, 2005.

  1. joram gemma

    joram gemma Guest

    Hello,

    on windows python 2.4.1 I have the following problem

    >>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
    >>> print s

    D:\music\D\Daniel Lanois\For the beauty of Wynona
    >>> t = 'D:\\music\\D\\'
    >>> print t

    D:\music\D\
    >>> s.lstrip(t)

    'aniel Lanois\\For the beauty of Wynona'
    >>>


    why does lstrip strip the D of Daniel Lanois also?

    thanks in advance
    joram
     
    joram gemma, May 15, 2005
    #1
    1. Advertising

  2. joram gemma

    Roy Smith Guest

    "joram gemma" <> wrote:

    > Hello,
    >
    > on windows python 2.4.1 I have the following problem
    >
    > >>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
    > >>> print s

    > D:\music\D\Daniel Lanois\For the beauty of Wynona
    > >>> t = 'D:\\music\\D\\'
    > >>> print t

    > D:\music\D\
    > >>> s.lstrip(t)

    > 'aniel Lanois\\For the beauty of Wynona'
    > >>>

    >
    > why does lstrip strip the D of Daniel Lanois also?


    Because the argument to lstrip is a *set of characters* to delete, not a
    string to delete. The string you passed it contained a 'D', so the 'D' got
    stripped. Imagine lstrip was defined something like:

    def lstrip (self, chars):
    temp = self
    while temp[0] in chars:
    temp = temp[1:]
    return temp

    and you should get the idea. I don't think the documentation for lstrip
    really makes this clear. I'm going to open a bug on the doc, and see what
    happens :)

    I suspect what you really want to be doing is using the os.path module.
    It's got functions for tearing pathnames apart into components, and hides
    all the uglyness like whether / or \ is the directory separator on your
    particular system.
     
    Roy Smith, May 15, 2005
    #2
    1. Advertising

  3. On Sun, 15 May 2005 15:24:25 +0200, "joram gemma" <> wrote:

    >Hello,
    >
    >on windows python 2.4.1 I have the following problem
    >
    >>>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
    >>>> print s

    >D:\music\D\Daniel Lanois\For the beauty of Wynona
    >>>> t = 'D:\\music\\D\\'
    >>>> print t

    >D:\music\D\
    >>>> s.lstrip(t)

    >'aniel Lanois\\For the beauty of Wynona'
    >>>>

    >
    >why does lstrip strip the D of Daniel Lanois also?
    >

    Because the lstrip argument is a set of characters in the form of
    a string, not a single substring to replace from the left. Note:
    (repeating your example to start with)

    >>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
    >>> print s

    D:\music\D\Daniel Lanois\For the beauty of Wynona
    >>> t = 'D:\\music\\D\\'
    >>> print t

    D:\music\D\
    >>> s.lstrip(t)

    'aniel Lanois\\For the beauty of Wynona'

    Now we make an equivalent lstrip argument from your t argument
    >>> t2 = ''.join(sorted(set(t)))
    >>> print t2

    :D\cimsu

    Note that there is only one of each character in t2 (e.g. 'D' and '\\')
    And the result is the same for t and t2:

    >>> s.lstrip(t)

    'aniel Lanois\\For the beauty of Wynona'
    >>> s.lstrip(t2)

    'aniel Lanois\\For the beauty of Wynona'

    If you want to replace an exact prefix, a regex could be a simple way
    to get the startswith check and replace in one whack.

    Regards,
    Bengt Richter
     
    Bengt Richter, May 15, 2005
    #3
  4. On Sun, 15 May 2005 15:24:25 +0200, "joram gemma"
    <> declaimed the following in comp.lang.python:

    >
    > why does lstrip strip the D of Daniel Lanois also?
    >

    Because lstrip() does NOT strip a PREFIX string.

    The characters you supply, individually, are considered
    "strippable".

    >>>help("".lstrip)

    Help on built-in function lstrip:

    lstrip(...)
    S.lstrip([chars]) -> string or unicode

    Return a copy of the string S with leading whitespace removed.
    If chars is given and not None, remove characters in chars instead.
    If chars is unicode, S will be converted to unicode before stripping

    lstrip() will remove characters until it finds one that is NOT
    IN the argument.

    >>> s ="D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona"
    >>> t ="\\Dmscui:"
    >>> s.lstrip(t)

    'aniel Lanois\\For the beauty of Wynona'
    >>>


    "a" is the first character that does not appear in string t; if you want
    to remove a fixed prefix, you need to match on the string itself.

    >>> t = "D:\\music\\D\\"
    >>> if s.startswith(t):

    .... s = s[len(t):]
    ....
    >>> s

    'Daniel Lanois\\For the beauty of Wynona'
    >>>


    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, May 15, 2005
    #4
  5. joram gemma

    M.E.Farmer Guest

    Roy Smith:
    > I suspect what you really want to be doing is using the os.path

    module.
    > It's got functions for tearing pathnames apart into components, and

    hides
    > all the uglyness like whether / or \ is the directory separator on

    your
    > particular system.



    I thought so too, maybe this will help.
    >>> d = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
    >>> import os
    >>> os.path.split(d)

    ('D:\\music\\D\\Daniel Lanois', 'For the beauty of Wynona')
    >>> os.path.basename(d)

    'For the beauty of Wynona'
    >>> os.path.dirname(d)

    'D:\\music\\D\\Daniel Lanois'
    >>> os.path.splitext(d)

    ('D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona', '')
    >>> os.path.splitdrive(d)

    ('D:', '\\music\\D\\Daniel Lanois\\For the beauty of Wynona')
    >>> os.path.split(d)

    ('D:\\music\\D\\Daniel Lanois', 'For the beauty of Wynona')
    >>> q = os.path.split(d)
    >>> q

    ('D:\\music\\D\\Daniel Lanois', 'For the beauty of Wynona')
    >>> q = os.path.split(q[0])
    >>> q

    ('D:\\music\\D', 'Daniel Lanois')
    >>> q = os.path.split(q[0])
    >>> q

    ('D:\\music', 'D')

    And another idea might be to do a split using os.sep:
    >>> import os
    >>> d = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
    >>> d.split(os.sep)

    ['D:', 'music', 'D', 'Daniel Lanois', 'For the beauty of Wynona']

    Hth,
    M.E.Farmer
     
    M.E.Farmer, May 16, 2005
    #5
    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. SB
    Replies:
    0
    Views:
    377
  2. Pete Jereb

    bizarre behavior using .lstrip

    Pete Jereb, Sep 20, 2003, in forum: Python
    Replies:
    8
    Views:
    339
    Peter Hansen
    Sep 20, 2003
  3. Brian

    Bug in string.lstrip?

    Brian, Aug 23, 2004, in forum: Python
    Replies:
    3
    Views:
    1,170
    Brian Gough
    Aug 23, 2004
  4. Kelie
    Replies:
    2
    Views:
    268
    Kelie
    Mar 29, 2008
  5. cpp4ever
    Replies:
    3
    Views:
    378
    Francesco
    Sep 8, 2009
Loading...

Share This Page