How to solve this problem using Spyder in Anaconda (Python 3)?

I am 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: codec 'ascii' cannot decode byte 0xe2 at position 6987: serial number is not in range (128)

From the Internet, I found that it should be because the encoding must be installed on utf-8, but my problem is that it is already in utf-8.

sys.getdefaultencoding() 
Out[43]: 'utf-8'

Also, it looks like my file is in utf-8, so I really got confused. In addition, the following code works:

In [15]: path = 'ch02/usagov_bitly_data2012-03-16-1331923249.txt'
In [16]: open(path).readline()

Is there any way to solve this problem?

Thank!

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 )

Do you know what could go wrong?

+4
2

-ascii -. - , 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"))]

( , )

+2

:

# -*- coding: utf-8 -*-

( u ', , ). , , .

0

Source: https://habr.com/ru/post/1659914/


All Articles