472,353 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

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

Similar topics

16
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error......
12
by: François | last post by:
Hi, After looking up Microsoft's site for licensing info...and found nothing, I turn to this newsgroup. Does someone know what version of VB .Net...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft...
12
by: johnny.karlsson | last post by:
Hi, I'm working on a project were a need to be able to upload firmware to a microcontroller based Ethernet device. But because of the memory...
9
by: Evil Bastard | last post by:
Hi, Has anyone done any serious work on producing a subset of python's language definition that would suit it to a tiny microcontroller...
3
by: Unemployed VC++ guy | last post by:
I have been playing around with C#, and rading some books: "Understanding ..NET" by David Chappell, and "Fast Track C#", by Wrox. The Chapell book...
8
by: K. Shier | last post by:
when i have the following code inside the .vb file that defines 'Public MustInherit Class frmDataEntry', if i try to open a derived form based on...
13
by: Jan | last post by:
Hi: I'm working on my first SQL Server-backend application and am already running into trouble. This is an application that has run successfully...
12
by: howa | last post by:
any side effect for PHP? what do you think?
1
by: btreddy | last post by:
Hii all , When i was trying to access the webpages from the server(localhost only ) I got the message "The current identity...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.