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

Dan Bishop danb_83 at yahoo.com
Mon Dec 31 03:54:05 EST 2007


On Dec 31, 2:20 am, "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?
>
> Thank you.

def ascii_chars(string):
    return ''.join(char for char in string if ord(char) < 128)



More information about the Python-list mailing list