This post matches my question in MySQL in Python: UnicodeEncodeError: 'ascii' , this is just to clarify the situation.
I try to save a string in a MySQL database, but I get an error:
File .smart.py, line 51, in (number, text, smart, u)
UnicodeDecodeError: codec 'ascii' cannot decode byte 0xc2 at position 25: serial number not in range (128)
and the string is stored in m ['Text']
Lala * = # &% @ <> _:;?! - '"/ () ¥ ¡¿
Here is a snippet of code
risk = m['Text'] msg = risk.encode('utf8') text = db.escape_string(msg) sql = "INSERT INTO posts(nmbr, \ msg, tel, sts) \ VALUES ('%s', '%s', '%s', '%s')" % \ (number, text, 'smart', 'u')
If I try to comment on the SQL query and set the print text , it prints Lala * = # &% @ <> _?! ;; - '"/ () ¥ ¡¿
The error only occurs when processing SQL.
MySQL encoding is set to utf8_unicode_ci. (or should I change that?)
Thanks.