MySQL: 'latin-1' codec can't encode character

Walter Dörwald walter at livinglogic.de
Fri May 13 12:05:14 EDT 2005


Fredrik Lundh wrote:

> [...]
> if you want more control of the replacement, you can skip the translate
> step and use your own error handler, e.g.
> 
>     charmap = ... see above ...
> 
>     def fixunicode(info):
>         s = info.object[info.start:info.end]
>         try:
>             return charmap[ord(s)], info.end

This will fail if there's more than one consecutive unencodable 
character, better use
    return charmap[ord(s[0])], info.start+1
or
    return "".join(charmap.get(ord(c), u"<U+%04x>" % ord(c)) for c in 
s), info.end
(without the try:) instead.

Bye,
    Walter Dörwald



More information about the Python-list mailing list