Python3 中如何处理中文编码问题
requests 解析 json 数据,在终端可以执行,但是在后台运行报错,如下:
‘ascii’ codec can’t encode characters in position 530-531: ordinal not in range(128)
json 中有中文 应该是这个引起的。
json() 之前 encode(‘utf-8’)也没法解决
这种情况下怎么解决?
Python3 中如何处理中文编码问题
那你要先搞清楚字符串到底是什么编码
在Python3里处理中文编码其实挺简单的,核心就记住一句话:所有字符串都是Unicode的str对象,读写文件或网络数据时明确指定编码。
具体操作上,主要注意这几个点:
-
代码文件编码:在
.py文件开头加# -*- coding: utf-8 -*-声明,或者直接用UTF-8保存文件。现在IDE和编辑器基本默认都是UTF-8了。 -
字符串处理:你直接写
s = "你好世界",这个s就是Unicode字符串,可以随意进行分割、拼接等操作。 -
文件读写:这是最容易出问题的地方。用
open()时,务必加上encoding参数。# 写入文件 with open('test.txt', 'w', encoding='utf-8') as f: f.write("一些中文内容") # 读取文件 with open('test.txt', 'r', encoding='utf-8') as f: content = f.read() print(content) # 正常显示中文如果不指定
encoding,它会用系统默认编码(比如Windows上是gbk),很容易就报UnicodeDecodeError。 -
网络请求或外部数据:拿到字节数据(
bytes)后,用.decode('正确的编码')转成字符串。# 假设从某个接口拿到gbk编码的字节数据 data_bytes = b'\xc4\xe3\xba\xc3' text = data_bytes.decode('gbk') # 解码为字符串 print(text) # 输出:你好反过来,发送数据时用
.encode('utf-8')把字符串转成字节。 -
遇到编码错误:如果碰到
UnicodeDecodeError,别慌。先确认数据源的编码是什么(可能是utf-8、gbk、gb2312等)。如果实在不确定,可以用chardet库检测一下,或者尝试用errors='ignore'或errors='replace'参数忽略无法解码的部分(但这会丢失数据)。
总结:统一使用UTF-8,并在所有I/O边界明确指定编码。
coding=utf-8
or
# -- coding:utf-8 --
后台文件有么?
脚本之前是 ok 的 现在 json 中的数据变了 应该是某个中文字符引起的
Python3 默认是 unicode 吧
Python 脚本没有指定编码,所以解释器就报错了吧,应该不是 Json 的锅。
$ ipython
Python 3.6.0 |Anaconda 4.3.1 (x86_64)| (default, Dec 23 2016, 13:19:00)
Type “copyright”, “credits” or “license” for more information.
IPython 5.1.0 – An enhanced Interactive Python.
? -> Introduction and overview of IPython’s features.
%quickref -> Quick reference.
help -> Python’s own help system.
object? -> Details about ‘object’, use ‘object??’ for extra details.
In [1]: import json
In [2]: json.dumps(‘你好’)
Out[2]: ‘"\u4f60\u597d"’
可以是国人的网页没跟足标准, 试试 resp.encoding=‘utf-8’:
url = '…'
resp = requests.get(url)
resp.encoding = ‘UTF-8’
=====
In [189]: url = 'http://www.baidu.com’
In [190]: import requests
In [191]: resp = requests.get(url)
In [192]: resp.encoding
Out[192]: ‘ISO-8859-1’ # bad !
In [200]: chardet.detect(resp.content)
Out[200]: {‘confidence’: 0.99, ‘encoding’: ‘utf-8’}
In [201]: resp.encoding = ‘utf-8’


