string.lstrip stripping too much?

J

joram gemma

Hello,

on windows python 2.4.1 I have the following problem

why does lstrip strip the D of Daniel Lanois also?

thanks in advance
joram
 
R

Roy Smith

joram gemma said:
Hello,

on windows python 2.4.1 I have the following problem


why does lstrip strip the D of Daniel Lanois also?

Because the argument to lstrip is a *set of characters* to delete, not a
string to delete. The string you passed it contained a 'D', so the 'D' got
stripped. Imagine lstrip was defined something like:

def lstrip (self, chars):
temp = self
while temp[0] in chars:
temp = temp[1:]
return temp

and you should get the idea. I don't think the documentation for lstrip
really makes this clear. I'm going to open a bug on the doc, and see what
happens :)

I suspect what you really want to be doing is using the os.path module.
It's got functions for tearing pathnames apart into components, and hides
all the uglyness like whether / or \ is the directory separator on your
particular system.
 
B

Bengt Richter

Hello,

on windows python 2.4.1 I have the following problem


why does lstrip strip the D of Daniel Lanois also?
Because the lstrip argument is a set of characters in the form of
a string, not a single substring to replace from the left. Note:
(repeating your example to start with)
'aniel Lanois\\For the beauty of Wynona'

Now we make an equivalent lstrip argument from your t argument :D\cimsu

Note that there is only one of each character in t2 (e.g. 'D' and '\\')
And the result is the same for t and t2:
'aniel Lanois\\For the beauty of Wynona'

If you want to replace an exact prefix, a regex could be a simple way
to get the startswith check and replace in one whack.

Regards,
Bengt Richter
 
D

Dennis Lee Bieber

why does lstrip strip the D of Daniel Lanois also?
Because lstrip() does NOT strip a PREFIX string.

The characters you supply, individually, are considered
"strippable".
Help on built-in function lstrip:

lstrip(...)
S.lstrip([chars]) -> string or unicode

Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping

lstrip() will remove characters until it finds one that is NOT
IN the argument.

"a" is the first character that does not appear in string t; if you want
to remove a fixed prefix, you need to match on the string itself.
.... s = s[len(t):]
....
--
 
M

M.E.Farmer

Roy Smith:
I suspect what you really want to be doing is using the os.path module.
It's got functions for tearing pathnames apart into components, and hides
all the uglyness like whether / or \ is the directory separator on your
particular system.


I thought so too, maybe this will help.
d = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
import os
os.path.split(d) ('D:\\music\\D\\Daniel Lanois', 'For the beauty of Wynona')
os.path.basename(d) 'For the beauty of Wynona'
os.path.dirname(d) 'D:\\music\\D\\Daniel Lanois'
os.path.splitext(d) ('D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona', '')
os.path.splitdrive(d) ('D:', '\\music\\D\\Daniel Lanois\\For the beauty of Wynona')
os.path.split(d) ('D:\\music\\D\\Daniel Lanois', 'For the beauty of Wynona')
q = os.path.split(d)
q ('D:\\music\\D\\Daniel Lanois', 'For the beauty of Wynona')
q = os.path.split(q[0])
q ('D:\\music\\D', 'Daniel Lanois')
q = os.path.split(q[0])
q
('D:\\music', 'D')

And another idea might be to do a split using os.sep:['D:', 'music', 'D', 'Daniel Lanois', 'For the beauty of Wynona']

Hth,
M.E.Farmer
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top