zuperakos
zuperakos

Reputation: 353

Python ascii encoding issue

I run a python script and i receive the following error

sql = 'insert into posts(msg_id, msg_text, msg_date) values("{0}", "{1}", "{2}")'.format(msg['id'], text.encode('utf-8'), msg['datime'])
UnicodeEncodeError: 'ascii' codec can't encode characters in position 25-31: ordinal not in range(128)

How can i correct this error or maybe caught it with an exception? Any ideas?

Upvotes: 1

Views: 289

Answers (1)

zmo
zmo

Reputation: 24812

try:

sql = u'insert into posts(msg_id, msg_text, msg_date) values("{0}", "{1}", "{2}")'.format(msg['id'], text.decode('utf-8'), msg['datime'])

basically, your text contains utf-8 characters, and using the encode() method, you keep it as is. But the main string (the ones you're formatting) is a plain ASCII string. By adding u in front of the string (u'') you make it a unicode string. Then, whatever being in text, you want to have it decoded as utf-8, thus the .decode() instead of .encode().

and if you want to catch that kind of errors, simply:

try:
    sql = …
except UnicodeEncodeError, err:
    print err

but if you want to really get rid of any utf8/ascii mess, you should think of switching to python 3.

HTH

Upvotes: 1

Related Questions