By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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


+1

Michael

Jul 19 '05 #2

P: n/a
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', "")

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

Jul 19 '05 #3

P: n/a
On 29 Apr 2005 21:27:18 -0700, "M.E.Farmer" <me*****@hotmail.com> wrote:
Bengt Richter wrote:
Just thought None as the first argument would be both handy andmnemonic,
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
Jul 19 '05 #4

P: n/a
M.E.Farmer 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', "")

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

s = "NHoBwA RyAoSuB AsAeHeH AiBtH,A CnRoAwD HyBoAuC HdRoCnH'AtB"
s.translate(None, "BADCHARS") "Now you see it, now you don't" s.replace("BADCHARS", "") "NHoBwA RyAoSuB AsAeHeH AiBtH,A CnRoAwD HyBoAuC HdRoCnH'AtB"

i. e. you have to replace() for every char in "BADCHARS":
reduce(lambda x, y: x.replace(y, ""), "BADCHARS", s)

"Now you see it, now you don't"
+1, btw.

Peter
Jul 19 '05 #5

P: n/a
[Bengt Richter]
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')


Log a feature request on SF and assignment to me.
I'll put this in for you.
Raymond Hettinger
Jul 19 '05 #6

P: n/a
After I re-read your post in daylight and read your followup the "Aha!"
hit me .I am a little slow at times. I have always just iterated thru
the badchars and replaced with "" . What you suggest would be very
nice indeed!
Thanks for the time and explanation, and you too Peter !

For what it is worth +1 ;)

On another note why is maketrans in the string module....
I was a bit lost finding it the first time should it be builtin?
transtable = "abcdefg".maketrans("qwertyu")
Probably missing something.
M.E.Farmer

Jul 19 '05 #7

P: n/a
On Sat, 30 Apr 2005 08:44:21 GMT, "Raymond Hettinger" <vz******@verizon.net> wrote:
[Bengt Richter]
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')


Log a feature request on SF and assignment to me.
I'll put this in for you.

Thanks. It has request ID # 1193128

Regards,
Bengt Richter
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.