2016-05-23 29 views
0

我想開Python中的JSON文件和我有錯誤:的UnicodeDecodeError當導入JSON文件

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 64864: ordinal not in range(128)

我的代碼非常簡單:

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

import json 

with open('birdw3l2.json') as data_file:  
    data = json.load(data_file) 
print(data) 

有人能幫助我嗎?謝謝!

回答

0

請嘗試下面的代碼。

import json 

with open('birdw3l2.json') as data_file:  
    data = json.load(data_file).decode('utf-8') 
print(data) 
+0

不會改變任何事情:( – jiayi

+0

@jiayi嘗試更新的一個 –

+0

沒有它總是同樣的問題。其實,即使我不做打印,錯誤仍然存​​在...... – jiayi

0

你應該指定你的編碼格式,當你加載你的json文件。像這樣:

data = json.load(data_file, encoding='utf-8') 

編碼取決於您的文件編碼。