Encodign issue in Python 3.3.1 (once again)

Michael Torrie torriem at gmail.com
Tue May 28 10:34:25 EDT 2013


On 05/27/2013 02:17 PM, Νίκος Γκρ33κ wrote:
> I have checked the database through phpMyAdmin and it is indeed UTF-8.
> 
> I have no idea why python 3.3.1 chooses to work with latin-iso only....

It's not python that is doing this here...

If you look at the source code to pymysql, I'm sure you will identify
the problem.  In fact I'll even walk you through things.

The traceback you posted tells you what's going on and why, with
superfluous details removed for clarity:

File "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line
108, in execute:
     query = query.encode(charset)
UnicodeEncodeError: 'latin-1' codec can't encode characters in position
46-52: ordinal not in range(256)

So there we have it.  pymysql is actually explicitly calling .encode()
on a string and is using whatever character set is specified by the
local variable charset.  If you look in cursors.py at that line, and
then a few lines above, you will find that charset is assigned to
conn.charset.  This means that the charset is actually defined in the
connection object.  So go look at connections.py.  Sure enough, it shows
that charset is defined by default as "latin-1."  That's no good for
you.  So take a look at the __init__ method in connections.py.  In there
you should find the necessary keyword argument you need to use when
creating the mysql connection to make sure the charset is utf-8.




More information about the Python-list mailing list