[Tutor] ascii codec cannot encode character

Dave Angel davea at ieee.org
Fri Jan 28 09:43:26 CET 2011


On 01/-10/-28163 02:59 PM, Alex Hall wrote:
> Hello again:
> I have never seen this message before. I am pulling xml from a site's
> api and printing it, testing the wrapper I am writing for the api. I
> have never seen this error until just now, in the twelfth result of my
> search:
> UnicodeEncodeError: 'ASCII' codec can't encode character u'\u2019' in
> position 42: ordinal not in range(128)
>
> I tried making the strings Unicode by saying something like
> self.title=unicode(data.find("title").text)
> but the same error appeared. I found the manual chapter on this, but I
> am not sure I want to ignore since I do not know what this character
> (or others) might mean in the string. I am not clear on what 'replace'
> will do. Any suggestions?
>

You don't show any of your code, nor the complete error message.  But I 
can try to correct one confusion on your part.

Encoding is converting from unicode to ascii.  So manually converting to 
unicode isn't going to help.  The question is what part of your code is 
doing an implicit conversion to ascii, and how can you change that to 
either leave things in unicode, or do an explicit encode to some other 
character set (utf8, or ...)

Also, you should be specifying your environment (python version, OS) in 
such a query, as the rules are substantially different in version 3.x

DaveA


More information about the Tutor mailing list