Is there a string function to trim all non-ascii characters out of a string

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Dec 31 06:05:47 EST 2007


On Mon, 31 Dec 2007 01:09:09 -0800, John Machin wrote:

> On Dec 31, 7:20 pm, "silverburgh.me... at gmail.com"
> <silverburgh.me... at gmail.com> wrote:
>> Hi,
>>
>> Is there a string function to trim all non-ascii characters out of a
>> string?
>> Let say I have a string in python (which is utf8 encoded), is there a
>> python function which I can convert that to a string which composed of
>> only ascii characters?
>>
>>
> OK, I'll bite: why do you want to throw data away?

Maybe he has to send the data to a device that can't deal with more than 
7-bit ASCII.

Maybe he's sick of seeing text with "missing character" squares all over 
from all the characters that his fonts can't display.

Maybe the string ends up as a file name on an operating system that 
doesn't support unicode.

Or maybe he's just a curmudgeon who thinks life was better when there 
were only 128 characters available.


-- 
Steven



More information about the Python-list mailing list