Reputation: 138
I'm using an Active Record called Storm in my Python code to retrieve some records on a MySQL database.
The problem is: My table is in 'utf8_unicode_ci', but when I retrieve the object I get 'latin-1' attributes, so I need to do object.attr.decode('latin-1').encode('utf-8') wich isn't working always - throwing some exceptions.
My question: This is a python behavior? a MySQL behavior? Something related to the Storm?
The code:
Storm.conn(user=db_user,db=db_name, passwd=db_passwd)
events = Event.select('*',status='=2',date_end='>=NOW()')
for event in events:
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
try:
#here we need only utf-8 strings
conn.index({"title": event.get_title(), "local": event.get_local(), "url": event.getUrl(), "description": event.get_description(), "artists": event.get_artists(), "tags": event.get_tags(), "picture": event.get_Picture(), "type": event.get_type(), 'date_begin': event.get_date_begin(), 'date_end': event.get_date_end(), '_ttl': event.get_ttl()}, "wf", "event", event.id)
print 'Indexed - '+now+': '+str(event.id)
except Exception,error:
print 'Error - '+now+': '+str(event.id)+" - "+str(error)
Upvotes: 3
Views: 110
Reputation: 50190
No idea about the specifics of your stack, but with MySQL you need to set the encoding of the connection separately from the encoding of the tables. I had some unicode tables and it took me a long time to realize that the connection was set to latin-1, so my unicode data got interpreted as Latin-1 bytes and "converted" to nonsense unicode at the far end.
Upvotes: 2
Reputation: 787
Maybe this answer seems stupid, but try to print this in the top of your py file after interpreter path
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Upvotes: 1