[Tutor] UnicodeEncodeError

Kent Johnson kent37 at tds.net
Fri Oct 3 20:23:44 CEST 2008


On Fri, Oct 3, 2008 at 1:50 PM, Rob Sutherland <luckdancing at gmail.com> wrote:
> I'm working on a python application that stores email in a postgresql
> database and
> I'm encountering the UnicodeEncodeError - while storing a particular
> email I receive
> this error
>
> UnicodeEncodeError: 'charmap' codec can't encode character u'\u2019'
> in position 144: character maps to <undefined>
>
> I'm a little confused about a workaround for this, from what I've
> googled the approach seems to be to use the codecs.register_error and
> codecs.ignore_errors to skip processing of the offending character. I
> haven't been able to find an understandable example though, so if
> anyone has one that would be great.

It helps if you show the code that is causing the error and the full
traceback. Presumably you are calling someString.encode("some
encoding") where "some encoding"  is an encoding that doesn't include
the character "U+2019 right single quotation mark"
http://www.eki.ee/letter/chardata.cgi?ucode=2019

The string encode() method takes a second argument which indicates how
errors should be handled. See the docs for the options:
http://docs.python.org/library/stdtypes.html#str.encode

Kent


More information about the Tutor mailing list