Re: Micro-PEP: str.translate(None) to mean identity translation



On 29 Apr 2005 21:27:18 -0700, "M.E.Farmer" <mefjr75@xxxxxxxxxxx> wrote:

>Bengt Richter wrote:
>> Just thought None as the first argument would be both handy and
>mnemonic,
>> signifying no translation, but allowing easy expression of deleting
>characters,
>> e.g.,
>>
>> s = s.translate(None, 'badcharshere')
>>
>> Regards,
>> Bengt Richter
>
>What's wrong with :
>
>s = s.replace('badchars', "")
That's not what translate does with the badchars, which is a set of
characters, each and all of whose instances in the source string
will be deleted from the source string. Something like
for c in badchars:
s = s.replace(c,'')
>
>It seems obvious to replace a char ( to me anyway ) with a blank
>string, rather than to 'translate' it to None.
>I am sure you have a reason what am I missing ?
>M.E.Farmer
The first argument is normally a 256-character string that serves as a table
for converting source characters to destination, so s.translate(table, bad)
does something like
s = ''.join([table[ord(c)] for c in s if c not in bad]

>>> help(str.translate)
Help on method_descriptor:

translate(...)
S.translate(table [,deletechars]) -> string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.

So just to delete, you wind up constructinf a table argument that is just 1:1 as in

>>> 'abcde'.translate(''.join([chr(i) for i in xrange(256)]), 'tbd')
'ace'

and the answer to the question, "What translation does such a table do?" is "None" ;-)

Regards,
Bengt Richter
.



Relevant Pages

  • Re: find and remove "" character from string
    ... i have the following string s and the following code, ... remaining characters have been mapped through the given translation table, which must be a string of length 256". ... For Unicode objects, the translatemethod does not accept the ...
    (comp.lang.python)
  • Re: Product Key
    ... The functions won't do creation and translation of a specific key; ... chosen characters to a specific set of characters. ... <MS ACCESS MVP> ... how I would use it to translate a string into a key. ...
    (microsoft.public.access.modulesdaovba)
  • Re: pop up a TCHAR[1024]
    ... Send me your paycheck and I'll send you the modified code. ... null characters, that's where the ... // other than the one we added at the end above, the string will not ... // after the translation. ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: pop up a TCHAR[1024]
    ... characters, that's where the ... // other than the one we added at the end above, the string will not ... // after the translation. ... best regards, ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Multi-Language Site
    ... a meaningfull string would be a better option IMO, ... GetTrans(StringId, LanguageId) function, to fetch the wanted translated ... and also request them by page (less data to request = ... you might consider storing all translation data in the Application ...
    (microsoft.public.inetserver.asp.db)