Python输出用 替换非ASCII字符 [英] Python output replaces non ASCII characters with �

查看:193
本文介绍了Python输出用 替换非ASCII字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 2.7从MySQL表中读取数据。在MySQL中,名称如下所示:


Garasa,Ángel。


但是当我在Python中打印时,输出是


Garasa, ngel


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

我已经尝试解码,但是收到如下错误:


UnicodeDecodeError:'utf8'编码解码器无法解码位置8中的字节0xc1:
无效起始字节


我已经阅读了关于解码和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:

Garasa, Ángel.

But when I print it in Python the output is

Garasa, �ngel

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:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xc1 in position 8: invalid start byte

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字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆