string.rstrip

J

John Hunter

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. Hun'

If you want to remove the extension, the best way is to use splitext
'test'

Hope this helps,
John Hunter
 
H

Hank

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.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
 
E

Erik Max Francis

Hank said:
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.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/lib/module-string.html
help(''.rstrip)
 
J

John J. Lee

Erik Max Francis said:
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
 
P

Peter Otten

Hank said:
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.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
'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:
.... if s.endswith(suffix):
.... return s[:-len(suffix)]
.... return s
....'test'

However, I guess that what you really want is to remove the filename
extension:
('test', '.txt')
'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.
('test.tar', '.gz')


Peter
 
M

Matthew Wilson

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

Peter Hansen

Matthew said:
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
 
A

Alan Kennedy

Matthew said:
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,
 
R

Raymond Hettinger

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top