0
votes

I am using a stored procedure to get some rows from mysql databse.

I using very basic syntax:

retarr = []
cursor.callproc("employee_get", (-1, -1, -1))
for res in cursor.stored_results():
    retarr.append(res.fetchall())
cursor.close()
...

Now. Some of data results are unicode strings. I use UTF-8 for encoding string in my database. I use charset="utf8" and use_unicode=True in my connection.

The problem occurs when i try to print any catched data (whole data tuple) which contain unicode characters. Like:

for row in retarr[0]:
    print(row)

it prints me the first catched row properly. But the first has no unicode character. Then comes the second and all i get is an famous error: UnicodeEncodeError: 'ascii' codec can't encode character '\u0119' in position 19: ordinal not in range (128). What's the problem? I believe i am missing something very basic.

I use Python 3.3.2, standard mysql connector. OSX.

2
It might be your terminal system. For example you can get UnicodeEncodeError on cmd.exe where it works fine in IDLE. - cdarke
Strange, it might be a problem with Komodo, i use. If i try to print simply '\u0119' from terminal (python3) then it works, but running it from Komodo throws me that exception. I changed default enconding to UTF-8 in preferences, but it still does not work. - Tomasz Szkudlarek

2 Answers

1
votes

To print a unicode object, it needs to be encoded somehow. If your default encoding is ASCII, that will fail for anything outside of the 128 ASCII chars. You probably need to specify an encoding:

print(row.encode('utf-8'))
1
votes

Problem was in the PYTHONIOENCODING environmental variable. When i set it in /etc/launchd.conf (file was absent on my filesystem) and rebooted my system all the problems gone away.