469,282 Members | 2,175 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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(mystr, ".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 20403
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(mystr, ".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.com> 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*********@yahoo.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.splitext('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(mystr, ".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(suffix):
.... return s[:-len(suffix)]
.... return s
.... removeSuffix(mystr, ".txt") 'test'

However, I guess that what you really want is to remove the filename
extension:
import os
os.path.splitext(mystr) ('test', '.txt')
os.path.splitext(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.splitext("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*********@yahoo.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.splitext('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_asdf = s[:-len("_asdf")]

of course, that works only if you know it's there... if not,
prepending with a test for s.endswith("_asdf") 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('_asdf$')
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Helmut Jarausch | last post: by
4 posts views Thread by Andy | last post: by
3 posts views Thread by steve morin | last post: by
7 posts views Thread by Steven D'Aprano | last post: by
3 posts views Thread by Ameet Nanda | last post: by
2 posts views Thread by kyo guan | last post: by
2 posts views Thread by dirkheld | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.