Problem with string parsing

M

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
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?
 
R

Remy Blank

Mike said:
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.
 
S

Simon Brunning

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:
'spam***###eggs'

Make sense now?
 
T

Thomas Guettler

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
 
R

Russell Blau

Mike Howard said:
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".
 
W

wes weston

Mike said:
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
 
P

Peter L Hansen

wes said:
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
'10-May-2002'

-Peter
 
M

Michael J. Fromberger

Peter L Hansen said:
wes said:
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
'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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top