473,556 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strip module bug


I'm using versions 2.5.2 and 2.5.1 of python and have encountered a
potential bug. Not sure if I'm misunderstandin g the usage of the strip
function but here's my example.

var = "detail.xml "
print var.strip(".xml ") ### expect to see 'detail', but get 'detai'
var = "overview.x ml"
print var.strip(".xml ") ### expect and get 'overview'

I have a work around using the replace function which happens to be the
better choice for my script anyhow. But am curious about the strip module.
Any thoughts? Is it removing the 'l' in detail because the strip function
text ends in 'l'?
Oct 13 '08 #1
4 2281
Poppy wrote:
>
I'm using versions 2.5.2 and 2.5.1 of python and have encountered a
potential bug. Not sure if I'm misunderstandin g the usage of the strip
function but here's my example.

var = "detail.xml "
print var.strip(".xml ") ### expect to see 'detail', but get 'detai'
var = "overview.x ml"
print var.strip(".xml ") ### expect and get 'overview'

I have a work around using the replace function which happens to be the
better choice for my script anyhow. But am curious about the strip module.
Any thoughts? Is it removing the 'l' in detail because the strip function
text ends in 'l'?
The behaviour you intend, stripping a suffix, is achieved by
>>var = "detail.xml "
suffix = ".xml"
if var.endswith(su ffix):
.... var = var[:-len(suffix)]
....

The var.strip(chars ) /method/ does something different. It treats chars as a
set of characters and removes any of these characters from the end and the
beginning of the var string, i. e.

"detail.xml " d is not in ".xml" -remove no further chars from the
beginning
"detail.xml " l is in ".xml", remove it
"detail.xm" m is in ".xml", remove it
"detail.x" x is in ".xml", remove it
"detail." . is in ".xml", remove it
"detail" l is in ".xml", remove it
"detai" i is not in ".xml", we're done

Peter
Oct 13 '08 #2
Poppy:
var = "detail.xml "
print var.strip(".xml ") ### expect to see 'detail', but get 'detai'
var = "overview.x ml"
print var.strip(".xml ") ### expect and get 'overview'
Python V.2.5 is not flawless, but you can't find bugs so easily. I've
found only two bugs in about three years of continuous usage (while I
have found about 27 new different bugs in the DMD compiler in about
one year of usage).

str.strip() isn't a module, and generally in Python it's called
"method", in this case a method of str (or unicode).

str.strip() as optional argument doesn't take the string you want to
remove, but a string that represent the set of chars you want to strip
away from both ends, so:
>>"detail.xml". strip("lmx.")
'detai'
>>"detail.xml". strip("abcdlz")
'etail.xm'

To strip the ending ".xml" you can use the str.partition() method, or
an alternative solution:
>>var = "detail.xml "
suffix = ".xml"
if var.endswith(su ffix):
.... var2 = var[: -len(suffix)]
....
>>var2
'detail'

Bye,
bearophile
Oct 13 '08 #3
I'm using versions 2.5.2 and 2.5.1 of python and have encountered a
potential bug. Not sure if I'm misunderstandin g the usage of the strip
function but here's my example.

var = "detail.xml "
print var.strip(".xml ") ### expect to see 'detail', but get 'detai'
var = "overview.x ml"
print var.strip(".xml ") ### expect and get 'overview'
..strip() takes a *set* of characters to remove, as many as are found:
_______ _____
>>'xqxqxqxyxxyx qxqx'.strip('xq ')
'yxxy'

Using .replace() may work, but might have some odd side effects:
>>'something.xm l.zip'.replace( '.xml')
'something.zip'
>>'this file has .xml in its name.xml'.repla ce('.xml')
'this file has in its name'

This also has problems if your filename and extension differ in
case (removing ".xml" from "file.XML")

If you want to remove the extension, then I recommend using the
python builtin:
>>import os
results = os.path.splitex t('this has .xml in its name.xml')
results
('this has .xml in its name', '.xml')
>>fname, ext = results
Or, if you know what the extension is:
>>fname = 'this file has .xml in its name.xml'
ext = '.xml'
fname[:-len(ext)]
'this file has .xml in its name'

Just a few ideas that are hopefully more robust.

-tkc


Oct 13 '08 #4
"Poppy" <zn************ ****@yahoo.comw rote:
>
Thanks Steven and Tim, I understand the strip module a lot more today.
It's NOT a module. It is a method of string objects.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 15 '08 #5

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

Similar topics

8
9337
by: Fazer | last post by:
Hello, I was wondering what would be the easiest way to strip away HTML tags from a string? Or how would I remove everything between < and > also the < , > as well using regex? Thanks for any help!
5
3298
by: qwweeeit | last post by:
Hi all, I need to limit as much as possible the lenght of a source line, stripping white spaces (except indentation). For example: .. . max_move and AC_RowStack.acceptsCards ( self, from_stack, cards ) must be reduced to: .. . max_move and AC_RowStack.acceptsCards(self,from_stack,cards) My solution has been (wrogly):...
4
1804
by: nephish | last post by:
hey there, i have a script that retrieves my email, but i need it to be able to strip all the stuff off except the body (the message itself) so i can later write it to a text file. anyone know how to accomplish this? thanks
9
8881
by: Julie Miles | last post by:
I need to pull several tables of data from Excel into a web page, but when I use Excel's "Save as web page" function, I get an enormous file containing a massive amount of css formatting. I'd like to strip out this css so that I can apply my own style sheet; does anyone know whether there is a way to do this, other than manually? Either a way...
6
2412
by: Markus Rosenstihl | last post by:
Hi, I wonder if i can make this nicer: In my case a list is created from a bill which is a tab seperated file and each line looks like this: "Verbindungen Deutsche Telekom vom 03.08.2005 bis 10.08.2005" "ISDN" "Deutsche Telekom AG" "Rufnummer" "123456789" "3" "Verbindungen zu T-Mobile AktivPlus xxl...
6
2549
by: rtilley | last post by:
s = ' qazwsx ' # How are these different? print s.strip() print str.strip(s) Do string objects all have the attribute strip()? If so, why is str.strip() needed? Really, I'm just curious... there's a lot don't fully understand :)
6
2343
by: eight02645999 | last post by:
hi can someone explain strip() for these : 'example' when i did this: 'abcd,words.words'
8
7005
by: Marko.Cain.23 | last post by:
Hi, I have a list of url names like this, and I am trying to strip out the domain name using the following code: http://www.cnn.com www.yahoo.com http://www.ebay.co.uk pattern = re.compile("http:\\\\(.*)\.(.*)", re.S)
6
1754
by: george | last post by:
hello, which is the best way to strip jscript/vbscript from user input? Is there any module I could reuse? thanks in advance george P.S. the solution must allow users to enter html code.
0
7547
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...
0
7828
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. ...
0
7907
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...
0
6178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5452
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2040
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
0
865
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...

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.