在Django中,我使用外部命令抛出对MySQL的查询。一切都变得很好,直到我恢复一个字符为utf-8字符(Cami ó n)。在那一点上,我不能再进入查询集,MySQL返回
'ascii' codec can't encode character u'\xf3' in position 34: ordinal not in range(128)
我的代码是下一个:
cursor = connection.cursor()
query = "SELECT * FROM " + table_name + " ORDER BY " + "1"
cursor.execute(query)
rows = cursor.fetchall()
for row in rows:
result = []
for field in row:
print field
result += [str(field)]
我该如何避免这种情况? 谢谢Mates ......
EDITED:
它适用于field = unicode(field).encode("utf-8", "replace")
答案 0 :(得分:1)
尝试在python脚本的开头添加以下行
# -*- coding: utf-8 -*-
答案 1 :(得分:0)
调用并替换下一个原始字段字符串... field = unicode(field).encode(“utf-8”,“replace”)