Filtering out non-readable characters

Bengt Richter bokr at oz.net
Sun Jul 24 01:03:56 EDT 2005


On Sat, 16 Jul 2005 23:28:02 -0400, "George Sakkis" <gsakkis at rutgers.edu> wrote:

>"Peter Hansen" <peter at engcorp.com> wrote:
>
>> George Sakkis wrote:
>> > "Peter Hansen" <peter at engcorp.com> wrote:
>> >>>> Where did you learn that, George?
>> >
>> > Actually I first read about this in the Cookbook; there are two or three
>>  > recipes related to string.translate. As for string.maketrans, it
>>  > doesn't do anything special for empty string arguments: ...
>>
>> I guess so.  I was going to offer to suggest a new paragraph on that
>> usage for the docs, but as you and Jp both seem to think the behaviour
>> is obvious, I conclude "it's just me" so I suppose I shouldn't bother.
>
>It's only obvious in the sense that _after_ you see this idiom, you can go back to the docs and
>realize it's not doing something special; OTOH if you haven't seen it, it's not at all the obvious
>solution to "how do I get the first 256 characters". So IMO it should be mentioned, given that
>string.translate often operates on the identity table. I think a single sentence is adequate for the
>reference docs.
>
I would suggest changing
"""
maketrans(from, to)
    Return a translation table suitable for passing to translate() or regex.compile(),
    that will map each character in from into the character at the same position in to;
    from and to must have the same length.
"""

to something that would make the idiom more easily inferrable, e.g.,

"""
maketrans(from, to)
    Return a translation table suitable for passing to translate() or regex.compile(),
    that will map each character in from into the character at the same position in to,
    while leaving all characters other than those in from unchanged;
    from and to must have the same length.
"""

Meanwhile, if my python feature request #1193128 on sourceforge gets implemented,
we'll be able to write s.translate(None, badchars) instead of having to build
an identity table to pass as the first argument. Maybe 2.5? (Not being pushy ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list