473,659 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string.lstrip stripping too much?

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 D:\music\D\Dani el Lanois\For the beauty of Wynona t = 'D:\\music\\D\\ '
print t D:\music\D\ s.lstrip(t) 'aniel Lanois\\For the beauty of Wynona'


why does lstrip strip the D of Daniel Lanois also?

thanks in advance
joram
Jul 19 '05 #1
4 3329
"joram gemma" <jo*********@pa ndora.be> wrote:
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 D:\music\D\Dani el Lanois\For the beauty of Wynona t = 'D:\\music\\D\\ '
print t D:\music\D\ s.lstrip(t) 'aniel Lanois\\For the beauty of Wynona'


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.
Jul 19 '05 #2
On Sun, 15 May 2005 15:24:25 +0200, "joram gemma" <jo*********@pa ndora.be> wrote:
Hello,

on windows python 2.4.1 I have the following problem
s = 'D:\\music\\D\\ Daniel Lanois\\For the beauty of Wynona'
print sD:\music\D\Dan iel Lanois\For the beauty of Wynona t = 'D:\\music\\D\\ '
print tD:\music\D\ s.lstrip(t)'aniel Lanois\\For the beauty of Wynona'
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)
s = 'D:\\music\\D\\ Daniel Lanois\\For the beauty of Wynona'
print s D:\music\D\Dani el Lanois\For the beauty of Wynona t = 'D:\\music\\D\\ '
print t D:\music\D\ s.lstrip(t) 'aniel Lanois\\For the beauty of Wynona'

Now we make an equivalent lstrip argument from your t argument t2 = ''.join(sorted( set(t)))
print t2 :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:
s.lstrip(t) 'aniel Lanois\\For the beauty of Wynona' s.lstrip(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
Jul 19 '05 #3
On Sun, 15 May 2005 15:24:25 +0200, "joram gemma"
<jo*********@pa ndora.be> declaimed the following in comp.lang.pytho n:

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("".lstr ip) 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.
s ="D:\\music\\D\ \Daniel Lanois\\For the beauty of Wynona"
t ="\\Dmscui:"
s.lstrip(t) 'aniel Lanois\\For the beauty of Wynona'
"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.
t = "D:\\music\\D\\ "
if s.startswith(t) : .... s = s[len(t):]
.... s 'Daniel Lanois\\For the beauty of Wynona'


-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 19 '05 #4
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.basenam e(d) 'For the beauty of Wynona' os.path.dirname (d) 'D:\\music\\D\\ Daniel Lanois' os.path.splitex t(d) ('D:\\music\\D\ \Daniel Lanois\\For the beauty of Wynona', '') os.path.splitdr ive(d) ('D:', '\\music\\D\\Da niel 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: import os
d = 'D:\\music\\D\\ Daniel Lanois\\For the beauty of Wynona'
d.split(os.sep)

['D:', 'music', 'D', 'Daniel Lanois', 'For the beauty of Wynona']

Hth,
M.E.Farmer

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2861
by: Pete Jereb | last post by:
Python 2.3 (#46, Jul 29 2003, 18:54:32) on win32 >>> s = 'chg bonn_fee' >>> print s chg bonn_fee >>> print s.lstrip('chg ') bonn_fee >>> s = 'chg conn_fee'
0
1552
by: Uche Ogbuji | last post by:
nikita_raja@yahoo.com (Sam Smith) wrote in message news:<b19beb92.0403051302.456e024f@posting.google.com>... > def characters(self, content): > self.content = self.content.lstrip().rstrip() + " " + content FYI, you can just use the following, which is equivalent: self.content = self.content.strip() + u" " + content Notice how I also maintain the Unicode object character of self.content
6
1411
by: Rigga | last post by:
Hi, I am new to python and am working on a script that parses a file and loads the results in to variables for processing. I am having problems when it comes to data in the file that wraps over two lines i.e. las - lomas What i want to be able to do is to some how strip all the spaces from it
3
2261
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 >>> string.lstrip("test/one/two/a.b.c", "test/one/") 'wo/a.b.c' >>> string.lstrip("test/one/two/a.b.c", "test/one") 'wo/a.b.c' >>> string.lstrip("test/one/two/a.b.c", "test/one/tw") 'a.b.c'
12
16092
by: neutrino | last post by:
Greetings to the Python gurus, I have a binary file and wish to see the "raw" content of it. So I open it in binary mode, and read one byte at a time to a variable, which will be of the string type. Now the problem is how to print the binary format of that charater to the standard output. It seems a common task but I just cannot find the appropriate method from the documentation. Thanks a lot.
4
2210
by: Andy | last post by:
What do people think of this? 'prefixed string'.lchop('prefix') == 'ed string' 'string with suffix'.rchop('suffix') == 'string with ' 'prefix and suffix.chop('prefix', 'suffix') == ' and ' The names are analogous to strip, rstrip, and lstrip. But the functionality is basically this: def lchop(self, prefix):
18
2416
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative description of how each works. I've been trying Google for the last 20 minutes but can't get anything decent. Thanks.
3
3380
by: steve morin | last post by:
http://www.python.org/doc/2.4.1/lib/node110.html These methods are being deprecated. What are they being replaced with? Does anyone know? Steve
7
3134
by: manstey | last post by:
Hi, How do I convert a string like: a="{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}" into a dictionary: b={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'} Thanks, Matthew
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.