[Python-ideas] More user-friendly version for string.translate()

Mikhail V mikhailwas at gmail.com
Wed Oct 26 20:32:55 EDT 2016


On 27 October 2016 at 00:17, Chris Barker <chris.barker at noaa.gov> wrote:
> I"ve lost track of what (If anything) is actually being proposed here... so
> I"m going to try a quick summary:
>
>
> 1) an easy way to spell "remove all the characters other than these"
>
> I think that's a good idea. What with unicode having an enormous number of
> code points, it really does make sense to have a way to specify only what
> you want, rather than what you don't want.
>
> Back in the good old days of 1-byte chars, it wasn't hard to build up a full
> 256 element translate table -- not so much anymore. And one of the whole
> points of str.translate() is good performance.
>
>  a) a new method:
>
>    str.remove_all_but(sequence_of_chars)
>   (naming TBD)
>
> b) a new flag in translate (Kind of like the decode keywords)
>
>   str.translate(table, missing='ignore'|'remove')
>
>
> (b) has the advantage of adding translation and removal in one fell swoop --
> but if you only want to remove, then you have to make a translation table of
> 1:1 mappings = not hard, but a annoying:

Exactly that is the proposal. And for same exact reason that you point out,
I also can't give a comment what would be better. It would be indeed
quite strange from syntactical POV if I just want to remove "all except"
and must call translate(). So ideally both should exist I think.

Mikhail


More information about the Python-ideas mailing list