UnicodeError: ASCII encoding error: oridinal not in range(128)
Fredrik Lundh
fredrik at pythonware.com
Tue Jul 16 18:52:30 EDT 2002
Doru-Catalin Togea wrote:
> The same code fails when "tmplText" comes from an XML files which contains
> norwegian characters (latin-1 extension), like ø, in the data, not
> in the tags, with the following error message:
>
> Traceback (most recent call last):
> File "./python/genh1.py", line 335, in ?
> f.write(tmplText)
> UnicodeError: ASCII encoding error: oridinal not in range(128)
you're trying to write unicode strings to an 8-bit device,
without telling Python what encoding to use.
instead of guessing what to do with non-ASCII characters,
Python throws an exception.
> What kind of strings do write() and print() handle? Hopefully I can
> convert my strings to that type.
try:
f.write(tmplText.encode(encoding))
where encoding is the encoding used by your terminal (most
likely "iso-8859-1", aka latin 1).
for some additional notes, see:
http://effbot.org/zone/unicode-objects.htm
http://www.python.org/doc/current/tut/node5.html (section 3.1.3)
http://www.reportlab.com/i18n/python_unicode_tutorial.html
and
http://www.python.org/peps/pep-0100.html
</F>
More information about the Python-list
mailing list