473,471 Members | 2,613 Online
Bytes | Software Development & Data Engineering Community
Create 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 misunderstanding 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.xml"
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 2271
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 misunderstanding 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.xml"
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(suffix):
.... 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.xml"
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(suffix):
.... 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 misunderstanding 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.xml"
print var.strip(".xml") ### expect and get 'overview'
..strip() takes a *set* of characters to remove, as many as are found:
_______ _____
>>'xqxqxqxyxxyxqxqx'.strip('xq')
'yxxy'

Using .replace() may work, but might have some odd side effects:
>>'something.xml.zip'.replace('.xml')
'something.zip'
>>'this file has .xml in its name.xml'.replace('.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.splitext('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.comwrote:
>
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
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...
5
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,...
4
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...
9
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...
6
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...
6
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......
6
by: eight02645999 | last post by:
hi can someone explain strip() for these : 'example' when i did this: 'abcd,words.words'
8
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 =...
6
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
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...
0
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...
0
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,...
1
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.