473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 20654
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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)...
11
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
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 ' ...
3
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
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: ...
7
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':...
3
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...
2
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
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.