我有一个mysql数据库。我将charset设置为utf8
...
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
...
我用MySQLdb连接到python中的db
conn = MySQLdb.connect(host = "localhost",
passwd = "12345",
db = "db",
charset = 'utf8',
use_unicode=True)
当我执行一个查询时,响应是用“windows-1254”解码的。示例响应
curr = conn.cursor(MySQLdb.cursors.DictCursor)
select_query = 'SELECT * FROM users'
curr.execute(select_query)
for ret in curr.fetchall():
username = ret["username"]
print "repr-username; ", repr(username)
print "username; "username.encode("utf-8")
...
输出为
repr-username; u'\xc5\u0178\xc3\xbckr\xc3\xbc\xc3\xa7a\xc4\u0178l\xc3\xbcli'
username; şükrüçağlüli
当我用“windows-1254”打印用户名时,效果很好
...
print "repr-username; ", repr(username)
print "username; ", username.encode("windows-1254")
...
输出为
repl-username; u'\xc5\u0178\xc3\xbckr\xc3\xbc\xc3\xa7a\xc4\u0178l\xc3\xbcli'
username; şükrüçağlüli
当我尝试用其他字符,如西里尔字母,解码是改变了。我怎样才能预防呢?你知道吗
我认为在插入数据库时编码的项目是错误的。你知道吗
我推荐python-ftfy(来自https://github.com/LuminosoInsight/python-ftfy)(帮助我解决了一个类似的问题):
相关问题 更多 >
编程相关推荐