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?
Note that if I just strip 'chg' and not the one whitespace character
next to it I get the expected result:
print s.lstrip('chg')
conn
but extending the right quote by one space not only strips the
whitespace but the c:
print s.lstrip('chg ')
onn
Not really sure what's causing this, but it's making me change the
text parser I'm 600 lines into. 8 2765
Pete Jereb wrote: 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
As an alternative you could use re.sub() import re s = 'chg cbonn_fee' print re.sub('chg ', '', s)
cbonn_fee
On Friday 19 September 2003 8:11 pm, Peter Hansen wrote: Pete Jereb wrote: 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
whoops! Forgot the '^' in the regex... import re s = 'chg cbonn_fee' print re.sub('^chg ', '', s)
On Friday 19 September 2003 10:43 pm, Jeremy Dillworth wrote: As an alternative you could use re.sub() import re s = 'chg cbonn_fee' print re.sub('chg ', '', s)
cbonn_fee
On Friday 19 September 2003 8:11 pm, Peter Hansen wrote: Pete Jereb wrote: 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
On 19 Sep 2003 17:07:47 -0700, in article
<68**************************@posting.google.com >, Pete Jereb wrote: 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?
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. print s.lstrip('chg') conn
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. print s.lstrip('chg ') onn
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()
"Peter Hansen" <pe***@engcorp.com> wrote in message
news:3F***************@engcorp.com... 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.
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[i] == pre[i]:
i += 1
return s[i:] rempre(s, 'chg')
' conn' rempre(s, 'chg ')
'conn'
Terry J. Reedy
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.
On Fri, Sep 19, 2003 at 10:43:27PM -0400, Jeremy Dillworth wrote: As an alternative you could use re.sub()
import re s = 'chg cbonn_fee' print re.sub('chg ', '', s) 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 for s in ("chg conn_fee", "something else", "chg x", "chg", "chg "):
.... remove_prefix("chg ", s)
....
'conn_fee'
'something else'
'x'
'chg'
''
Jeff
Terry Reedy wrote: "Peter Hansen" <pe***@engcorp.com> wrote in message news:3F***************@engcorp.com... 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian |
last post by:
Hello - has anyone else had trouble with string.lstrip stripping too
much? Here's what I get (Python 2.3.4):
>>> import string
>>>...
|
by: joram gemma |
last post by:
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...
|
by: Michael Carr |
last post by:
I have an intermittent problem that occurs on a very small number of
browsers that access my website. The strange thing is this: my site works...
|
by: Michael Carr |
last post by:
I have created a website that responds to the following two domain names:
logintest.carr-engineering.com
logintest.onualumni.org
If you open...
|
by: Mark |
last post by:
We're authoring a VS 2005 app that includes several EXE's and DLL's and also
uses a COM component (a customer requirement). The COM component...
|
by: zoehart |
last post by:
I'm working with VBScript to build a text email message. I'm seeing a variety of bizarre formatting issues. The following lines of code
MT = MT &...
|
by: Peter |
last post by:
Hi!
I am having some very strange behavior with my databound controls. It's
taken a long time to isolate exactly what is provoking the problem,...
|
by: bukzor |
last post by:
I've found some bizzare behavior when using mutable values (lists,
dicts, etc) as the default argument of a function. I want to get the
community's...
|
by: Kelie |
last post by:
Hello,
Is there such a function included in the standard Python distribution?
This is what I came up with. How to improve it? Thanks.
def...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
| |