473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2314
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... document.frmKitAmount.txtTotalKitValue is null or not...
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 you are entitled to use when you are a micro...
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 struggled mightily to win that battle -- creating a...
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 constraints the controller can only handle packages of...
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 environment? In its full form, python is a resource...
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 does not mention any code that has a class being...
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 frmDataEntry, i get an error "Could not find any...
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 with a Jet backend, and I'm starting out by...
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 (SYSNAME\ASPNET) does not have write access to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.