Encoding error

Gerhard Häring gh at ghaering.de
Mon Jul 14 17:06:02 CEST 2003

Welcome to the Python community,

But please configure your mailer to send plain text only.

Casey Kohrt wrote:
> I get the following error for the list item below.  I know I have to 
> encode it, but am unsure how or where to write that in.  I am new to 
> python and have had good luck thus far.  Any help is greatly 
> apprecieated.  I am not on the list, so a response to me is appreciated.
> UnicodeError: ASCII encoding error: ordinal not in range(128)

The library you're using tries to convert a bytestring you gave it to a 
Unicode string. If your bytestring is plain ASCII (characters 0 to 127), 
this works. Outside the world of 7-bit ASCII it's not clear which code 
means which character, so you'll have to provide the encoding explicitly.

>     eainfo = doc.createElement("eainfo")
>     metadata.appendChild(eainfo)
>     overview = doc.createElement("overview")
>     eainfo.appendChild(overview)
>     eaover = doc.createElement("eaover")

>     text = doc.createTextNode(str(list[83]))

list[83] contains non-ASCII characters. Try creating a Unicode string 
instead and provide an explicit encoding:

text = doc.createTextNode(unicode(list[83], "iso-8859-1"))

I'm just guessing about your encoding, it might be a different one.

Also please next time when asking questions where you got an exception 
also provide at which line in your code the exception occured.

>     eaover.appendChild(text)
>     overview.appendChild(eaover)


-- Gerhard

More information about the Python-list mailing list