[英]How to solve this encoding issue in with Spyder in Anaconda (Python 3)?
I'm trying to run the following: 我正在尝试运行以下内容:
import json
path = 'ch02/usagov_bitly_data2012-03-16-1331923249.txt'
records = [json.loads(line) for line in open(path)]
But I get the following error : 但是我收到以下错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6987: ordinal not in range(128) UnicodeDecodeError:'ascii'编解码器无法解码位置6987中的字节0xe2:序数不在范围内(128)
From the internet I've found that it should be because the encoding needs to be set to utf-8, but my issue is that it's already in utf-8. 从互联网上我发现它应该是因为编码需要设置为utf-8,但我的问题是它已经在utf-8中了。
sys.getdefaultencoding()
Out[43]: 'utf-8'
Also, it looks like my file is in utf-8, so I'm really confused Also, the following code works : 此外,它看起来我的文件是在utf-8,所以我真的很困惑另外,以下代码工作:
In [15]: path = 'ch02/usagov_bitly_data2012-03-16-1331923249.txt'
In [16]: open(path).readline()
Is there a way to solve this ? 有办法解决这个问题吗?
Thanks ! 谢谢 !
EDIT: 编辑:
When I run the code in my console it works, but not when I run it in Spyder provided by Anaconda ( https://www.continuum.io/downloads ) 当我在我的控制台中运行代码时,它可以工作,但是当我在Anaconda提供的Spyder中运行它时( https://www.continuum.io/downloads )
Do you know what can go wrong ? 你知道什么可能出错吗?
The text file contains some non-ascii characters on a line somewhere. 文本文件在某处某行包含一些非ascii字符。 Somehow on your setup the default file encoding is set to ascii instead of utf-8 so do the following and specify the file's encoding explicitly: 在您的设置上,默认文件编码设置为ascii而不是utf-8,因此请执行以下操作并明确指定文件的编码:
import json
path = 'ch02/usagov_bitly_data2012-03-16-1331923249.txt'
records = [json.loads(line.strip()) for line in open(path, encoding="utf-8"))]
(Doing this is a good idea anyway even when the default works) (即使默认有效,这样做也是个好主意)
I try to ran this program with one additional line at the top: 我尝试在顶部添加一行来运行此程序:
# -*- coding: utf-8 -*-
It fetches the lines and shows the output (with u' prefixed strings; probably a conversion might be required after this). 它获取行并显示输出(使用u'前缀字符串;在此之后可能需要转换)。 But, it didn't throw any error as you mentioned. 但是,它没有像你提到的那样抛出任何错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.