Django, Python and UTF-8
Oi! Sometimes, people annoy me. The rest of the time, they can be awefully useless. Anyway, I’m working with Django on some stuff, and need to be able to cleanly handle UTF-8 characters. This shouldn’t be too hard, as Python has had Unicode support for quite a while. Unfortunately, changing the default character encoding is painful.
First, the solution is to put the following code in your _ _ init _ _.py for the site.
import sys # Have to futz with namespace because of idiocy reload(sys) sys.setdefaultencoding(‘utf-8’)
So, why the futzing? Someone decided that once site.py is loaded, you shouldn’t be able to fix up the encoding. I suppose this makes sense if you don’t trust anyone, but in the “real world,” it’s critical that the individual programs be allowed to choose their encodings. I’ve not found any other way to do this, unfortunately. If someone knows of a better way, please let me know.
n.b. The formatting is all hosed because the Textile processor on WordPress is so mind-numbingly stupid as to not understand that PRE tags should not be mangled, or that if something is inside a code tag, you don’t want it to translate italics.
This entry was posted at 5:20 pm on 18 February 2007 and is filed under Python. You can follow any responses to this entry through the post-specific RSS 2.0 feed.
No comments found.
You can leave a response, or trackback from your own site.