Deleting specific characters from a string

Bengt Richter bokr at oz.net
Wed Jul 9 19:44:02 EDT 2003


On Wed, 09 Jul 2003 23:36:03 +0200, Behrang Dadsetan <ben at dadsetan.com> wrote:

>Walter Dörwald wrote:
>
>> Behrang Dadsetan wrote:
>>
>>> Hi all,
>>>
>>> I would like deleting specific characters from a string.
>>> As an example, I would like to delete all of the '@' '&' in the 
>>> string 'You are ben at orange?enter&your&code' so that it becomes 
>>> 'benorange?enteryourcode'.
>>>
>>> So far I have been doing it like:
>>> str = 'You are ben at orange?enter&your&code'
>>> str = ''.join([ c for c in str if c not in ('@', '&')])
>>>
>>> but that looks so ugly.. I am hoping to see nicer examples to acheive 
>>> the above..
>>
>>
>> What about the following:
>>
>> str = 'You are ben at orange?enter&your&code'
>> str = filter(lambda c: c not in "@&", str)
Aaack! I cringe seeing builtin str name rebound like that ;-/

>>
>> Bye,
>>    Walter Dörwald 
>
>def isAcceptableChar(character):
>    return charachter in "@&"
     return character not in "@&"
>
>str = filter(isAcceptableChar, str)
>
>is going to finally be what I am going to use.
That's not going to be anywhere near as fast as Donn's translate version.

>I not feel lambdas are so readable, unless one has serious experience in 
>using them and python in general. I feel it is acceptable to add a named 
>method that documents with its name what it is doing there.
>
>But your example would probably have been my choice if I was more 
>familiar with that type of use and the potential readers of my code were 
>also familiar with it. Many thanks!
>
IMO, if you are going to define a function like isAcceptableChar, only to use it
with filter, why not write a function to do the whole job, and whose invocation
reads well, while hiding Donn's fast translate version? E.g., substituting the literal
value of string.maketrans('',''):

====< removechars.py >========================================================
def removeChars(s, remove=''):
    return s.translate(
        '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
        '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'
        ' !"#$%&\'()*+,-./'
        '0123456789:;<=>?'
        '@ABCDEFGHIJKLMNO'
        'PQRSTUVWXYZ[\\]^_'
        '`abcdefghijklmno'
        'pqrstuvwxyz{|}~\x7f'
        '\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f'
        '\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f'
        '\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf'
        '\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf'
        '\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf'
        '\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf'
        '\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef'
        '\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
        , remove)

if __name__ == '__main__':
    import sys
    args = sys.argv[1:]
    fin = sys.stdin; fout=sys.stdout; remove='' # defaults
    while args:
        arg = args.pop(0)
        if arg == '-fi': fin  = file(args.pop(0))
        elif arg == '-fo': fout = file(args.pop(0))
        else: remove = arg
    for line in fin:
        fout.write(removeChars(line, remove))
==============================================================================
Not tested beyond what you see here ;-)

 [16:40] C:\pywk\ut>echo "'You are ben at orange?enter&your&code'" |python removechars.py "@&"
 "'You are benorange?enteryourcode'" 

 [16:41] C:\pywk\ut>echo "'You are ben at orange?enter&your&code'" |python removechars.py aeiou
 "'Y r bn at rng?ntr&yr&cd'"

Copying a snip above to the clipboard and filtering that with no removes and then (lower case) vowels:

 [16:41] C:\pywk\ut>getclip |python removechars.py
 >I not feel lambdas are so readable, unless one has serious experience in
 >using them and python in general. I feel it is acceptable to add a named
 >method that documents with its name what it is doing there.
 >
 >But your example would probably have been my choice if I was more
 >familiar with that type of use and the potential readers of my code were
 >also familiar with it. Many thanks!

 [16:42] C:\pywk\ut>getclip |python removechars.py aeiou
 >I nt fl lmbds r s rdbl, nlss n hs srs xprnc n
 >sng thm nd pythn n gnrl. I fl t s ccptbl t dd  nmd
 >mthd tht dcmnts wth ts nm wht t s dng thr.
 >
 >Bt yr xmpl wld prbbly hv bn my chc f I ws mr
 >fmlr wth tht typ f s nd th ptntl rdrs f my cd wr
 >ls fmlr wth t. Mny thnks!


Regards,
Bengt Richter




More information about the Python-list mailing list