Unicode -> String problem

François Pinard pinard at iro.umontreal.ca
Sun Sep 8 17:20:25 EDT 2002

> Jay Parlar wrote:
>> [...] Is there a simple way to convert the unicode text to StringType,
>> removing the resulting unicode strings for unrepresentable characters?

[Paul Prescod]
>>>> import codecs
>>>> codecs.ascii_encode(u"foo\u3333bar", "ignore")
> ('foobar', 7)

The documentation for Python 2.2.1 does not speak about `ascii_encode',
and the interactive documentation is rather terse:

>>> help(codecs.ascii_encode)
Help on built-in function ascii_encode:



Where did you find that bit of information? :-)

For now, I would presume that `codecs.ascii_encode(UNICODE_STRING)' does the
same as `UNICODE_STRING.encode("ascii")' and consequently, we do not need to
know about `ascii_encode'.  Is that right?

François Pinard   http://www.iro.umontreal.ca/~pinard

More information about the Python-list mailing list