reload(sys)
Steven Bethard
steven.bethard at gmail.com
Thu Sep 6 12:19:14 EDT 2007
Sönmez Kartal wrote:
> I was using the XMLBuilder(xmlbuilder.py). I'm writing XML files as
> "f.write(str(xml))". At execution of that line, it gives error with
> description, configure your default encoding...
>
[and later]
>
> http://rafb.net/p/RfaF8215.html
>
> products in the code is a list of dictionaries which are returned by
> makeProduct function.
>
> I'm not typing or pasting those characters into my script. So,
> declaring an encoding didn't make it. :-( But, your code excerpt
> runned well.
Gabriel Genellina wrote:
> You should ensure that arguments to makeProduct are either:
> - unicode objects
> - ASCII strings
>
> If you got them from some other place, decode the strings as soon as
> possible into unicode. Read <http://www.amk.ca/python/howto/unicode>
> to understand what's happening
To further illustrate Gabriel's point, here is some code where I read in
some UTF8 text from a file. If you properly decode that text from UTF8,
you don't get any errors. If you forget to decode that text, you'll get
exactly the "default encoding" error you were getting before:
>>> f = open('temp.txt', 'w')
>>> f.write(u'® and ™'.encode('utf8'))
>>> f.close()
>>> non_decoded_text = open('temp.txt').read()
>>> decoded_text = non_decoded_text.decode('utf8')
>>> import xmlbuilder
>>> builder = xmlbuilder.XMLBuilder()
>>> builder.foo = dict(bar=non_decoded_text)
>>> str(builder)
Decoding Error: You must configure default encoding
>>> builder = xmlbuilder.XMLBuilder()
>>> builder.foo = dict(bar=decoded_text)
>>> str(builder)
'<?xml version="1.0" encoding="utf-8"?><foo><bar>\xc2\xae and
\xc2\x99</bar></foo>'
Note that I didn't have to do anything with the default encoding. I
simply had to decode the text file with the appropriate codec. So,
looking at your code, I'm guessing that you need to figure out where
you're reading in the "name", "url" and "image" values, and make sure
you're properly decoding that text.
STeVe
P.S. If you can find somewhere to file a bug report for XMLBuilder, you
really should. The error instructing you to configure the default
encoding is really just wrong.
More information about the Python-list
mailing list