Sean W.
Sean W.

Reputation: 5132

What could cause a UnicodeEncodeError exception to creep into a working Python environment?

I have a method in my script that pulls a Twitter RSS feed, parses it with FeedPharser, wraps it in TwiML (Twilio-flavored XML) using the twilio module, and returns the resulting response in a CherryPy method via str(). This works my fine in development environment (Kubuntu 10.10); I have had mixed results on my server (Ubuntu Server 10.10 on Linode).

For the first few months, all was well. Then, the method described above began to fail with something like:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 259: ordinal not in range(128)

But, when I run the exact same code on the same feed, with the same python version, on the the same OS, on my development box, the code executes fine. However, I should note that even when it does work properly, some characters aren't outputted right. For example:

’

rather than

'

To solve this anomaly, I simply rebuilt my VPS from scratch, which worked for a few more months, and then the error came back.

The server automatically installs updated Ubuntu packages, but so does my development box. I can't think of anything that could cause this. Any help is appreciated.

Upvotes: 2

Views: 242

Answers (2)

Sean W.
Sean W.

Reputation: 5132

A few reboots later (for unrelated reasons) and it's working again. How odd....

Upvotes: 0

Henry Hammond
Henry Hammond

Reputation: 173

XML data cannot contain certain characters. An easy workaround is to wrap the data inside your XML tag that is giving you the error with CDATA. For example:

<xmltag><![CDATA[Your content]]></xmltag>

Or you can use numerical reference values ex &amp; for &

More information on this is available here:

http://en.wikipedia.org/wiki/XML#Characters_and_escaping http://en.wikipedia.org/wiki/Numeric_character_reference http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references http://en.wikipedia.org/wiki/CDATA

Upvotes: 1

Related Questions