Encodign issue in Python 3.3.1 (once again)

Tim Roberts timr at probo.com
Sun May 26 22:01:17 EDT 2013


????? ???33? <nikos.gr33k at gmail.com> wrote:
>
>This is the code that although correct becaus it works with englisg(standARD ASCII letters) it wont with Greek:
>...
>if( log ):
>	name = log
>	# print specific client header info
>	cur.execute('''SELECT hits, money FROM clients WHERE name = %s''', (name,) )
>	data = cur.fetchone()
>=======================
>
>The following is the live output of: tail -F /usr/local/apache/logs/error_log &
>...
>   File "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute, referer: http://superhost.gr/cgi-bin/pelatologio.py
>     query = query.encode(charset), referer: http://superhost.gr/cgi-bin/pelatologio.py
> UnicodeEncodeError: 'latin-1' codec can't encode characters in position 46-52: ordinal not in range(256), referer: http://superhost.gr/cgi-bin/pelatologio.py
>
>I can udnerstand that this is an encoding issue but i dont knwo how to fix this.
>please help.

While the other responders have a good laugh at your expense, let me
actually point you toward a solution.

The traceback line I left above is the key.  It shows that the pymysql
module is trying to encode your Unicode string into an 8-bit character set
in order to send it to the MySQL server.  It is THAT conversion that
failed; this has nothing to do with the server (yet).

So, why did it choose 'latin-1'?  Because that's the default character set
for pymysql.  You could have learned this yourself -- you have the full
source code for pymysql on your machine.

You can override the default character set:

    con = pymysql.connect( db = 'metrites', host = 'localhost', user =
'me', passwd = 'somepass', charset='utf-8', init_command='SET NAMES UTF8' )
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list