bizarre behavior using .lstrip

P

Pete Jereb

Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32
onn_fee


Does this make any sense at all? where did the lead c in conn_fee go?

Note that if I just strip 'chg' and not the one whitespace character
next to it I get the expected result:
conn

but extending the right quote by one space not only strips the
whitespace but the c:
onn


Not really sure what's causing this, but it's making me change the
text parser I'm 600 lines into.
 
P

Peter Hansen

Pete said:
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32
onn_fee

Does this make any sense at all? where did the lead c in conn_fee go?

Based on the behaviour you describe, I would assume lstrip()
removes, starting at the beginning of the string, all characters
which are *anywhere* in the argument string you give it, until
it encounters a character not in that string, at which point it
stops.

-Peter
 
J

Jeremy Dillworth

As an alternative you could use re.sub()
cbonn_fee


Pete said:
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32
s = 'chg bonn_fee'
print s

chg bonn_fee
print s.lstrip('chg ')
bonn_fee

s = 'chg conn_fee'
print s

chg conn_fee
print s.lstrip('chg ')

onn_fee

Does this make any sense at all? where did the lead c in conn_fee go?

Based on the behaviour you describe, I would assume lstrip()
removes, starting at the beginning of the string, all characters
which are *anywhere* in the argument string you give it, until
it encounters a character not in that string, at which point it
stops.

-Peter
 
J

Jeremy Dillworth

whoops! Forgot the '^' in the regex...


As an alternative you could use re.sub()

cbonn_fee

Pete said:
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32

s = 'chg bonn_fee'
print s

chg bonn_fee

print s.lstrip('chg ')

bonn_fee

s = 'chg conn_fee'
print s

chg conn_fee

print s.lstrip('chg ')

onn_fee

Does this make any sense at all? where did the lead c in conn_fee go?

Based on the behaviour you describe, I would assume lstrip()
removes, starting at the beginning of the string, all characters
which are *anywhere* in the argument string you give it, until
it encounters a character not in that string, at which point it
stops.

-Peter
 
J

Jules Dubois

On 19 Sep 2003 17:07:47 -0700, in article
onn_fee

Does this make any sense at all? where did the lead c in conn_fee go?

It was removed, as you requested. lstrip('chg ') means remove every
occurence of space, 'g', 'h', and 'c' from the front (left) of the string.

lstrip('chg') stops stripping when it finds a character not in 'chg'. The
space is the first character not 'chg' so that's where lstrip stops
stripping.

lstrip('chg ') stops stripping when it finds a character not in 'chg '.
The 'o' is the first character not in 'chg' so that's where lstrip stops
stripping.
Not really sure what's causing this [...]

As they said where I used to work, WAD: it Works As Designed.
[...] but it's making me change the
text parser I'm 600 lines into.

It's not clear what you're trying to do. Do you want to remove all the
leading space, 'g', 'h' and 'c' characters from the string, or do you want
to remove the leading (exact) string "chg " as a whole?

One way to remove the leading 'g', 'h', and 'c' characters from the front
of the string first, and then remove spaces from the resulting string

print s.lstrip('chg').lstrip()
 
T

Terry Reedy

Peter Hansen said:
go?

Based on the behaviour you describe, I would assume lstrip()
removes, starting at the beginning of the string, all characters
which are *anywhere* in the argument string you give it, until
it encounters a character not in that string, at which point it
stops.

Good call: from Lib Ref 2.2.6.1 String Methods

lstrip( [chars])

Return a copy of the string with leading characters removed. If chars
is omitted or None, whitespace characters are removed. 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.

If one wants to remove a leading substring, s[k:] does nicely.
Determining a common prefix to remove is also easy. The following
does what OP expected:

def rempre(s, pre):
'assume len(pre) <= len(s)'
stop = len(pre)
i = 0
while i < stop and s == pre:
i += 1
return s[i:]
'conn'

Terry J. Reedy
 
P

Pete Jereb

Thanks to all for the clarification. I was under the impression that
lstrip would remove a string I specified, not any character in the
string until a character is no longer found. I like the

s.lstrip('chg').lstrip

solution.

This is my first Python program, and between the messages on this
group and "Python in a Nutshell" I've been able to solve all the
problems I've encountered thus far. Hopefully I'll be able to
contribute to this group in a little bit! Pete.
 
J

Jeff Epler

As an alternative you could use re.sub()

cbonn_fee

I'd recommend avoiding regexes if this is all you're doing. Instead,
you can write a function to do it:

def remove_prefix(prefix, s):
"""remove_prefix(prefix, s) -> str
If s starts with prefix, return the part of s remaining after
prefix. Otherwise, return the original string"""
if s.startswith(prefix):
return s[len(prefix):]
return s
.... remove_prefix("chg ", s)
....
'conn_fee'
'something else'
'x'
'chg'
''

Jeff
 
P

Peter Hansen

Terry said:
Peter Hansen said:
go?

Based on the behaviour you describe, I would assume lstrip()
removes, starting at the beginning of the string, all characters
which are *anywhere* in the argument string you give it, until
it encounters a character not in that string, at which point it
stops.

Good call: from Lib Ref 2.2.6.1 String Methods

lstrip( [chars])

Return a copy of the string with leading characters removed. If chars
is omitted or None, whitespace characters are removed. 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.

I think that last phrase might be responsible for the OP's confusion.
Even now my brain interprets it as meaning the *string* formed by those
characters will be removed from the beginning of the string, not that
each occurrence of any character in that arg string will be removed
from the target.

-Peter
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top