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

Fredrik Lundh fredrik at pythonware.com
Fri May 13 11:45:51 EDT 2005


francescomoi at europe.com wrote:

> > > File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py",
> line
> > > 33, in defaulterrorhandler
> > >     raise errorclass, errorvalue
> > > UnicodeEncodeError: 'latin-1' codec can't encode character
> u'\u2026' in
> > > position 288: ordinal not in range(256)

> Thank you very much for your quick answer.
>
> Do you suggest to change it by using regexp or must I encode the whole
> texto into a suitable one?

a simple solution would be to manually create a table of problematic
unicode characters, use the translate method on the unicode string,
and then encode using the "replace" option.

    charmap = {
        0x2026: u"...",
        # ...
    }

    text = u'telephone\u2026'

    text = text.translate(charmap)
    text = text.encode("iso-8859-1", "replace")

    print text

    http://docs.python.org/lib/string-methods.html

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
        except KeyError:
            # fallback
            return u"<U+%04x>" % ord(s), info.end

    import codecs
    codecs.register_error("fixunicode", fixunicode)

    text = u'telephone\u2026'

    text = text.encode("iso-8859-1", "fixunicode")

hope this helps!

</F>






More information about the Python-list mailing list