python - Python 和 mySQL UnicodeDecodeError:“ascii”编解码器无法解码字节
问题描述
我真的需要你的帮助
我正在使用简单的 python 脚本从 mysql DB 中导出数据。但是,当我打印结果时,我在其中一个字段中得到了一些 UTF-8 字符,当我使用制表符打印它时,它给了我以下流行错误“UnicodeDecodeError:'ascii'编解码器无法解码字节”主要目标是打印数据库上的确切数据。
你建议如何解决这个问题?
任何帮助将不胜感激谢谢
编码:
# -*- coding: utf-8 -*-
mydb = mysql.connector.connect(
host="localhost",
user="user",
password="password",
database="database",
#charset='ascii' -> if i use this i can print with tabulate but some characters are replaced with ?
)
mycursor = mydb.cursor()
mycursor.execute("SELECT column1, column2, column3, column4, column5, column6, column7, column8, column9, column10, colum11 FROM database LIMIT 5")
myresult = mycursor.fetchall()
for x in myresult:
print ('\n')
print(x) # Here i can print the results with for example "Situa\xc3\xa7\xc3\xa3o com impacto em monitoria e despiste"
print(tabulate(myresult, headers=['column1', 'column2', 'column3', 'column4', 'column5', 'column6', 'column7', 'column8', 'column9', 'column10', 'colum11'], tablefmt='psql'))
# And here i get the error "... UnicodeDecodeError: 'ascii' codec can't decode byte... "
解决方案
推荐阅读
- angularjs - 在 AngularJS 输入字段上找不到 XPath 值
- angular - 无法使用 moment-mini-ts lib
- spring - 模型和视图弹簧
- python - 如何在ansible中工作?
- git - 如果没有先构建新创建的发布分支,GitVersion 不会在 TeamCity 中为开发分支构建次要版本
- php - 高级 PHP 循环
- postgresql - 如何使用 PL/pgSQL 将静态字符串附加到格式化数组
- php - 基于每个域用户的 PHP 配置在 fastcgi apache 服务器运行时不起作用
- javascript - 将键转换为大写
- wpf - Wpf MVVM 和编辑 DatagridCell