mysql查询以unicode格式返回数据

2024-06-09 15:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python和MySQL非常陌生,在执行查询时,我使用Python在数据库中转储了一个文件Select * from table_name 我得到的结果是Unicode格式的,即

(1, u'John',u'smith')
(2, u'Markus',u'Doe')
(3, u'Andrew',u'smith')

但是,我需要这种格式

(1, 'John','smith')
(2, 'Markus','Doe')
(3, 'Andrew','smith')

文件使用encoding=“utf-8”格式 由于这种unicode格式,我无法基于外键执行任何插入

此问题的python代码:

with codecs.open("input.txt", "r", encoding="utf-8") as f:
   for num,line in enumerate(f,1):
        try:

            words=line.split('\t')
            list =[]
            for word in words:
                list.append(word.encode("utf-8"))

            args = (list[0],list[0])
            cursor.execute(query,args)
f.close()
cursor.execute("Select * from table_name")
rows = cursor.fetchall()

print('Total Row(s):', cursor.rowcount)
for row in rows:
     print [type(word) for word in row]



conn.commit()         
conn.close()

请帮帮我,提前谢谢。你知道吗


Tags: 文件nameinfromfor格式tablejohn