问题描述
我使用Python 2.7从MySQL表中读取数据。在MySQL中,名称如下所示:但是当我在Python中打印时,输出是
MySQL中的字符集名称是utf8。
这是我的Python代码:
#encoding:utf-8
import MySQLdb
connection = MySQLdb.connect
(host =localhost,user =root,passwd =root,db =jmdb)
cursor = connection。 cursor $)
cursor.execute(select * from actors where actorid = 672462;)
data = cursor.fetchall()
for data in data:
printIMDB Name =,row [4]
wiki =(。join(row [4]))
print wiki
我已经尝试解码,但是收到如下错误:
我已经阅读了关于解码和UTF-8但是找不到解决方案。
获取Mysql驱动程序以返回Unicode字符串。这意味着您不必在代码中处理解码。
只需将 use_unicode = True
连接参数。如果表已经设置了特定的编码,则相应地设置 charset
属性。
I am using Python 2.7 to read data from a MySQL table. In MySQL the name looks like this:
But when I print it in Python the output is
The character set name in MySQL is utf8.This is my Python code:
# coding: utf-8
import MySQLdb
connection = MySQLdb.connect
(host="localhost",user="root",passwd="root",db="jmdb")
cursor = connection.cursor ()
cursor.execute ("select * from actors where actorid=672462;")
data = cursor.fetchall ()
for row in data:
print "IMDB Name=",row[4]
wiki=("".join(row[4]))
print wiki
I have tried decoding it, but get error such as:
I have read about decoding and UTF-8 but couldn't find a solution.
Get the Mysql driver to return Unicode strings instead. This means that you don't have to deal with decoding in your code.
Simply set use_unicode=True
in the connection parameters. If the table has been set with a specific encoding then set the charset
attribute accordingly.
这篇关于Python输出用 替换非ASCII字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!