dimanche 3 juillet 2016

How to handle "UnicodeEncodeError: 'ascii' codec can't encode character u'u2019' "?


I am trying to access a table on a SQL Server using the Python 2.7 module adodbapi, and print certain information to the command prompt (Windows). Here is my original code snippet: query_str = "SELECT id, headline, state, severity FROM GPS3.Defect ORDER BY id" cur.execute(query_str) dr_data = cur.fetchall() con.close() for i in dr_data: print i It will print out about 30 rows, all correctly formatted, but then it will stop and give me this: UnicodeEncodeError: 'ascii' codec can't encode character u'u2019' in position 52: oridinal not in range(128) So I looked this up online and went through the presentation explaining Unicode in Python, and I thought I understood. So I would explicitly tell the Python interpreter that it was dealing with Unicode, and should encode it into UTF-8. This is what i came up with: for i in dr_data: print (u"%s"%i).encode('utf-8') However, I suppose I don't actually understand Unicode because I get the same exact error when I run this. I know this question is asked a lot, but could someone explain to me, simply, what is going on here? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire