Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problem with string parsing

Reply
Thread Tools

Problem with string parsing

 
 
Mike Howard
Guest
Posts: n/a
 
      10-05-2004
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?
 
Reply With Quote
 
 
 
 
Remy Blank
Guest
Posts: n/a
 
      10-05-2004
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.

 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      10-05-2004
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/
 
Reply With Quote
 
Thomas Guettler
Guest
Posts: n/a
 
      10-05-2004
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



 
Reply With Quote
 
Russell Blau
Guest
Posts: n/a
 
      10-05-2004
"Mike Howard" <> wrote in message
news: om...
> 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.


 
Reply With Quote
 
wes weston
Guest
Posts: n/a
 
      10-05-2004
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

 
Reply With Quote
 
Peter L Hansen
Guest
Posts: n/a
 
      10-06-2004
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
 
Reply With Quote
 
Michael J. Fromberger
Guest
Posts: n/a
 
      10-06-2004
In article <CuWdnb3aJ90b0v7cRVn->,
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What libraries should I use for MIME parsing, XML parsing, and MySQL ? John Levine Ruby 0 02-02-2012 11:15 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 08:58 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57