Encodign issue in Python 3.3.1 (once again)

Terry Jan Reedy tjreedy at udel.edu
Sun May 26 16:53:56 EDT 2013


On 5/26/2013 12:36 PM, Νίκος Γκρ33κ 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 &
>
>
> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] Error in sys.excepthook:, referer: http://superhost.gr/cgi-bin/pelatologio.py
> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] UnicodeEncodeError: 'ascii' codec can't encode characters in position 2050-2056: ordinal not in range(128), referer: http://superhost.gr/cgi-bin/pelatologio.py
> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] , referer: http://superhost.gr/cgi-bin/pelatologio.py
> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] Original exception was:, referer: http://superhost.gr/cgi-bin/pelatologio.py
> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] Traceback (most recent call last):, referer: http://superhost.gr/cgi-bin/pelatologio.py
> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11]   File "pelatologio.py", line 122, in <module>, referer: http://superhost.gr/cgi-bin/pelatologio.py
> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11]     cur.execute('''SELECT hits, money FROM clients WHERE name = %s''', (name,) ), referer: http://superhost.gr/cgi-bin/pelatologio.py

> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11]   File "/opt/python3/lib/python3.3/site-packages/pymysql/cursors.py", line 108, in execute, referer: http://superhost.gr/cgi-bin/pelatologio.py
> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11]     query = query.encode(charset), referer: http://superhost.gr/cgi-bin/pelatologio.py
> [Sun May 26 19:24:04 2013] [error] [client 46.12.46.11] 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

This appears to be an issue with your mysql database and how you set it 
up.. It is using charset='latin-1' whereas you need it to consistently 
use charset='utf8' both for storing strings and for retrieval. Check the 
mysql manual or ask on a mysql list for how to do that. I have no idea 
so don't ask me, or send mail to my emall address.





More information about the Python-list mailing list