may be a bug in string.rstrip

Scott SA pydev at rscorp.ab.ca
Fri Nov 23 02:28:06 EST 2007


On 11/23/07, kyo guan (kyoguan at gmail.com) wrote:

>   Please look at this code:   
>
>>>> 'exe.torrent'.rstrip('.torrent')
>'ex'           <-----  it should be 'exe', why?
>
>but this is a right answer:
>
>>>> '120.exe'.rstrip('.exe')        
>'120'          <------ this is a right value.
>
>   there is a bug in the rstrip, lstrip there isn't this problem.

Since your error has been addressed, I'd like to offer a point not expressed by others (that I've seen yet).

To perform this task, there are a couple of options i.e. string.replace:

    >>> string.replace('120.exe','.exe','')
    '120'

... but it has a side-effect of mid-string replacements:

    >>> string.replace('123.exe.more','.exe','')
    '123.more'

or maybe:
    
    >>> if x[-4:] == '.exe': 
    ...     x[:-4]
    '123'

... is functional, but not elegant i.e. quick 'n dirty esp. as one line.

The better option, IMO, is probably to use regex. The module you would use is "re".

There are a lot of cool things you can do with regex, one of them in relation to your needs, is the ability to replace substrings:

    >>> import re
    >>> reg = re.compile('(.exe)$') # the $ means end of line
    >>> reg.sub('','123.exe')     
    '123'
    >>> reg.sub('','123.exe.more')  # test for mid-string placement
    '123.exe.more'

Clearly, it is more typing than your original string.strip approach. But as you see, it has the ability to expressly match what you were looking for.

Of course, it has the ability to do case-insensitive replacements too... and the elegance _starts_ to show here:

    >>> reg = re.compile('.(exe|ini|com|fred|wendy)$',re.IGNORECASE)
    >>> reg.sub('','123.exe')     
    '123'
    >>> reg.sub('','123.EXE') 
    '123'
    >>> reg.sub('','123.WenDy')
    '123'

    ... would you like fries with that?

Just imagine what this would look like using the previous non-regex examples, or maybe not as halloween was _last_ month (scary stuff indeed!).

Here are some links to some great docs 8^):

    <http://docs.python.org/lib/module-re.html>
    <http://www.amk.ca/python/howto/regex/>

S&R, what you want, is described in more detail here:

<http://www.amk.ca/python/howto/regex/regex.html#SECTION000620000000000000000>

And more links at:
    <http://www.google.com/search?q=python+regex>

HTH

Scott



More information about the Python-list mailing list