'ascii' codec can't encode character u'\u2013'

deelan ggg at zzz.it
Fri Sep 30 09:48:44 EDT 2005


thomas Armstrong wrote:
(...)
> when trying to execute a MySQL query:
> ----
> query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE
> id='" + id + "'"
> cursor.execute (query)  #<--- error line
> ----

well, to start it's not the best way to do an update,
try this instead:

query = "UPDATE blogs_news SET text = %s WHERE id=%s"
cursor.execute(query, (text_extrated, id))

so mysqldb will take care to quote text_extrated automatically. this
may not not your problem, but it's considered "good style" when dealing
with dbs.

apart for this, IIRC feedparser returns text as unicode strings, and
you correctly tried to encode those as latin-1 str objects before to 
pass it to mysql, but not all glyphs in the orginal utf-8 feed can be 
translated to latin-1. the charecter set of latin-1 is very thin 
compared to the utf-8.

you have to decide:

* switch your mysql db to utf-8 and encode stuff before
insertion to UTF-8

* lose those characters that cannot be mapped into latin-1,
using the:

text_extrated.encode('latin-1', errors='replace')

so unrecognized chars will be replaced by ?

also, mysqldb has some support to manage unicode objects directly, but 
things changed a bit during recent releases so i cannot be precise in 
this regard.

HTH.

-- 
deelan, #1 fan of adriana lima!
<http://www.deelan.com/>







More information about the Python-list mailing list