<br><br><div class="gmail_quote">On Mon, Jan 26, 2009 at 3:21 PM, Hans Müller <span dir="ltr"><<a href="mailto:heintest@web.de">heintest@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi python experts,<br>
<br>
in the moment I'm struggling with an annoying problem in conjunction with mysql.<br>
<br>
I'm fetching rows from a database, which the mysql drive returns as a list of tuples.<br>
<br>
The default coding of the database is utf-8.<br>
<br>
Unfortunately in the database there are rows with different codings and there is a blob<br>
column.<br>
<br>
In the app. I search for double entries in the database with this code.<br>
<br>
hash = {}<br>
cursor.execute("select * from table")<br>
rows = cursor.fetchall()<br>
for row in rows:<br>
        key = "|".join([str(x) for x in row])           <- here the problem arises<br>
        if key in hash:<br>
                print "found double entry"<br>
<br>
This code works as expected with python 2.5.2<br>
With 2.5.1 it shows this error:<br>
<br>
<br>
key = "|".join(str(x) for x in row)<br>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u017e' in position 3: ordinal<br>
not in range(128)<br>
<br>
When I replace the str() call by unicode(), I get this error when a blob column is being<br>
processed:<br>
<br>
key = "|".join(unicode(x) for x in row)<br>
<br>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 119: ordinal not in<br>
range(128)<br>
<br>
<br>
Please help, how can I convert ANY column data to a string which is usable as a key to a<br>
dictionary. The purpose of using a dictionary is to find equal rows in some database<br>
tables. Perhaps using a md5 hash from the column data is also an idea ?</blockquote><div><br>unicode takes an optional encoding argument. If you don't specify, it uses ascii. Try using (untested):<br><br>key = u"|".join(unicode(x, encoding="utf-8") for x in row)<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Thanks a lot in advance,<br>
<br>
Hans.<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>