Python 从MySQL读取unicode单词时出错
我是python新手,从mysql读取数据时遇到问题。在mysql中,我的表存储目标:Python 从MySQL读取unicode单词时出错,python,mysql,unicode,Python,Mysql,Unicode,我是python新手,从mysql读取数据时遇到问题。在mysql中,我的表存储目标: |id | Destination| |1 |Hà Nội | |2 |Hồ Chí Minh | 。。。 但当我从python中读到它时,它的返回是:HáN的u'H\xe0n\u1ed9i\'ộ我有没有办法把它转换回原来的字符串:“HáN?”ộ我 以下是我打开连接并选择数据的代码: def __init__(self): self.mySQLConnec
|id | Destination|
|1 |Hà Nội |
|2 |Hồ Chí Minh |
。。。
但当我从python中读到它时,它的返回是:HáN的u'H\xe0n\u1ed9i\'ộ我有没有办法把它转换回原来的字符串:“HáN?”ộ我
以下是我打开连接并选择数据的代码:
def __init__(self):
self.mySQLConnection = mysql.connector.connect(user='root', password='', host='localhost',database='traveltrend',charset='utf8',use_unicode=True)
self.cursor = self.mySQLConnection.cursor(buffered=True)
def getDest(self):
self.cursor.execute("SELECT ID, ThanhPho FROM diadiem ")
row = self.cursor.fetchone()
listDest={}
while row is not None:
request = str(row)
request = request.strip('()')
request = request.split(',')
encoding = "utf-8"
on_error = "replace"
place= request[1].encode(encoding,on_error)
我连接MySQLdb包。我用unicode字符读取MySQL,这对我很有用
from MySQLdb import connect
connection = connect(user='root', passwd='', host='localhost',db='traveltrend',use_unicode=True)
希望这有帮助 您可以发布用于创建表的查询吗?我使用php my admin创建表,但下面是生成的sql代码create table
destination
(ID
int(11)NOT NULL,ThanhPho
varchar(50)字符集utf8 COLLATE utf8_vietnamese_ci NOT NULL)ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_vietnamese_ci@arunI在尝试按您的方式执行时出错:TypeError:“use_unicode”是此函数的无效关键字参数,适用于MySQL python==1.2.5。你使用哪个版本?