Python,mysqldb和unicode

我无法从
mysql查询中获取unicode值.

我现在就是这样做的:

>>> from MySQLdb import connect
>>> from MySQLdb.cursors import DictCursor
>>> con = connect(
    passwd = "****",
    db = 'my_db',
    user = "db_user",
    host = "localhost",
    cursorclass = DictCursor,
    use_unicode=True,
    charset="utf8"
)
>>> cursor = con.cursor ()
>>> cursor.execute (u'Select * from basic_applet')
>>> res = cursor.fetchall()
>>> print(res)
({'Title_de': 'test title', .... })
>>> type(res[0]['Title_de'])
<type 'str'>

如您所见,它不会返回unicode.

在我的表结构中,Title_de被设置为unicode.

IDbasic_applet  int(10)...
Title_de    varchar(75)     utf8_bin

我真的不知道我做错了什么,任何帮助都会非常受欢迎.

提前致谢,

西蒙

最佳答案 你得到的是一个字节串.您必须对其进行解码才能获得unicode字符串.它基本上归结为:

>>> byte_string = 'F\xe9vrier'
>>> byte_string.decode('UTF-8')
u'Février'
点赞