Problem with string parsing

Michael J. Fromberger Michael.J.Fromberger at Clothing.Dartmouth.EDU
Wed Oct 6 16:19:51 EDT 2004


In article <CuWdnb3aJ90b0v7cRVn-hg at powergate.ca>,
 Peter L Hansen <peter at engcorp.com> 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



More information about the Python-list mailing list