473,574 Members | 2,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string.rstrip

Hi,

I have this problem with string.rstrip

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright" , "credits" or "license" for more information.
import string
mystr = "test.txt"
mystr = string.rstrip(m ystr, ".txt")
print mystr

tes

The last 't' was stripped. Is this fixed or documented anywhere? it
works for other cases, but this one seems to give me a weird result.

Thanks
Jul 18 '05 #1
8 20661
Hank wrote:
I have this problem with string.rstrip

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright" , "credits" or "license" for more information.
import string
mystr = "test.txt"
mystr = string.rstrip(m ystr, ".txt")
print mystr
tes

The last 't' was stripped.


What you mean is the result is 'tes', which is correct.
Is this fixed or documented anywhere? it
works for other cases, but this one seems to give me a weird result.


You're using .rstrip incorrectly. The second argument to string.rstrip
is a list of characters, any one of which will be stripped if they are
at the end of the string. So you're not saying, "remove .txt from the
end of the string," you're saying, "remove any of the characters ., t,
or x from the end of the string until you don't find anymore." Since
you give it test.txt, that means it strips everything up to the s, since
they're all one of those characters.

It's documented in the library reference as well as the interpreter help
facility.

http://www.python.org/doc/current/li...le-string.html
help(''.rstrip)

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Grab a club and join the chorus / Evolution is a state of mind
\__/ Oleta Adams
Jul 18 '05 #2
Erik Max Francis <ma*@alcyone.co m> writes:
[...]
You're using .rstrip incorrectly. The second argument to string.rstrip
is a list of characters, any one of which will be stripped if they are

[...]

Erik means 'a sequence of characters', of course, not a list.
John
Jul 18 '05 #3
>>>>> "Hank" == Hank <so*********@ya hoo.ca> writes:

Hank> The last 't' was stripped. Is this fixed or documented
Hank> anywhere? it works for other cases, but this one seems to
Hank> give me a weird result.

That's because rstrip removes any characters up until the first one
one that doesn't match. Since the 't' before the '.' is in the strip
string '.txt' it is stripped too. Does this example clarify:

'John D. Hunter'.rstrip( 'retD') 'John D. Hun'

If you want to remove the extension, the best way is to use splitext
import os
base, ext = os.path.splitex t('test.txt')
base

'test'

Hope this helps,
John Hunter

Jul 18 '05 #4
Hank wrote:
Hi,

I have this problem with string.rstrip

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright" , "credits" or "license" for more information.
import string
mystr = "test.txt"
mystr = string.rstrip(m ystr, ".txt")
print mystr tes

The last 't' was stripped. Is this fixed or documented anywhere? it
works for other cases, but this one seems to give me a weird result.


There are several issues with your code. The preferred way to invoke
rstrip() is now as a method, so
mystr = "test.txt"
mystr.rstrip(". txt") 'tes'

Why's that? Think of the argument of rstrip() as a character set rather than
a string; characters are removed from the end of mystr as long as they are
in [".", "t", "x"].

To remove an arbitrary suffix you can write your own function:
def removeSuffix(s, suffix): .... if s.endswith(suff ix):
.... return s[:-len(suffix)]
.... return s
.... removeSuffix(my str, ".txt") 'test'

However, I guess that what you really want is to remove the filename
extension:
import os
os.path.splitex t(mystr) ('test', '.txt')
os.path.splitex t(mystr)[0] 'test'

This returns both parts of the string and thus gives you the chance to check
if the extension is really what you expected.

One caveat: only the last extension is cut off the filename.
os.path.splitex t("test.tar.gz" )

('test.tar', '.gz')
Peter
Jul 18 '05 #5
What would be the best way to strip off the last "_asdf" from
"asdf_asdf_asdf "?

>> "Hank" == Hank <so*********@ya hoo.ca> writes:
Hank> The last 't' was stripped. Is this fixed or documented
Hank> anywhere? it works for other cases, but this one seems to
Hank> give me a weird result.

That's because rstrip removes any characters up until the first one
one that doesn't match. Since the 't' before the '.' is in the strip
string '.txt' it is stripped too. Does this example clarify:

>>> 'John D. Hunter'.rstrip( 'retD') 'John D. Hun'

If you want to remove the extension, the best way is to use splitext
>>> import os
>>> base, ext = os.path.splitex t('test.txt')
>>> base

'test'

Hope this helps,
John Hunter

--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #6
Matthew Wilson wrote:

What would be the best way to strip off the last "_asdf" from
"asdf_asdf_asdf "?


s = "asdf_asdf_asdf "
without_last_as df = s[:-len("_asdf")]

of course, that works only if you know it's there... if not,
prepending with a test for s.endswith("_as df") would do the trick.

-Peter
Jul 18 '05 #7
Matthew Wilson wrote:
What would be the best way to strip off the last "_asdf" from
"asdf_asdf_asdf "?


I don't about "the best way" (define "best"), but here's another way

#--------------
import re
patt = re.compile('_as df$')
mystring = "asdf_asdf_asdf "
print patt.sub('', mystring)
#--------------------------

HTH,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #8
[Matthew Wilson]
What would be the best way to strip off the last "_asdf" from
"asdf_asdf_asdf "?


Hmm, if they are contiguous, how would you know
which one got stripped? ;-)
Raymond Hettinger

Jul 18 '05 #9

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

Similar topics

0
1721
by: John F Dutcher | last post by:
Having 'cloned' an existing python script that imports 'string' and uses "string.rstrip()" without incident... I am at a loss to explain why the new 'cloned' script (brief sample below) continually errors with: NameError: global name 'string' is not defined args = ("global name 'string' is not defined",) when run from the server CGI-BIN. ...
11
2865
by: Helmut Jarausch | last post by:
Hi, entering help('rstrip') or help('ljust') into IDLE's shell window I only get no Python documentation found ...
4
2207
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):
3
3371
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
1684
by: Steven D'Aprano | last post by:
I have a sinking feeling I'm missing something really, really simple. I'm looking for a format string similar to '%.3f' except that trailing zeroes are not included. To give some examples: Float String 1.0 1
7
3128
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
3
1702
by: Ameet Nanda | last post by:
Hi All, I access net using a proxy, which I have to authenticate everytime I try to access net from my system. Now when I use urllib2.urlopen(url) , I cant get ahead. I must provide proxy authentication , I tried reading docs online which speak of something called as FancyUrlOpener. Now i want to hardcode my username and password inside the...
2
1885
by: kyo guan | last post by:
Hi : Please look at this code: 'ex' <----- it should be 'exe', why? but this is a right answer: '120' <------ this is a right value.
2
1582
by: dirkheld | last post by:
Hi, I wrote some python code that retrieves urls from a txt file. In this code I use .rstrip() for removing the '\n' at the end of every url. While this code works on my mac (leopard) with python 2.5.1, this same code fails to work on an ubuntu server with python 2.4.3 I was wondering if there is a problem with .rstrip() in that python...
0
8075
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
8253
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...
0
6471
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
5632
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
3752
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2253
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
1
1356
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1072
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.