Efficient, built-in way to determine if string has non-ASCII chars outside ASCII 32-127, CRLF, Tab?

Patrick Maupin pmaupin at gmail.com
Tue Nov 1 00:03:59 EDT 2011


On Oct 31, 9:12 pm, Dave Angel <d... at davea.name> wrote:

> I would claim that a well-written (in C) translate function, without
> using the delete option, should be much quicker than any python loop,
> even if it does copy the data.

Are you arguing with me?  I was agreeing with you, I thought, that
translate would be faster than a regex.  I also pointed out that the
delete function was available as , but OTOH, that might be a little
slow because I think it does the deletion before the translate.  I
think I'd just do something like this:

>>> transtab = ''.join(' ' if 32 <= x <= 126 else chr(x) for x in range(256))
>>> 'abc\x05\def\x0aghi'.translate(transtab).replace(' ', '')
'\x05\n'


Regards,
Pat



More information about the Python-list mailing list