2014-12-07 67 views
1

我想加载一个json文件到python中,但没有成功。过去几个小时我一直在使用Google搜索解决方案,但似乎无法加载。我试图使用已经适用于每个人的相同的json.load('文件名')函数加载它。我不断收到一个“UnicodeDecodeError:'utf8'编解码器无法解码位置124中的字节0xc2:无效的连续字节”。将.json加载到python中; UnicodeDecodeError

这里是我使用

import json 
json_data = open('myfile.json') 
for line in json_data: 
data = json.loads(line) <--I get an error at this. 

下面的代码是从我的文件

{"topic":"security","question":"Putting the Biba-LaPadula Mandatory Access Control Methods to Practise?","excerpt":"Text books on database systems always refer to the two Mandatory Access Control models; Biba for the Integrity objective and Bell-LaPadula for the Secrecy or Confidentiality objective.\n\nText books ...\r\n  "} 

采样线什么是我的错误,如果这似乎在每一个例子,我有曾经为大家一派?

+0

您将在循环的每个步骤中覆盖数据。也许你想使用'+ ='而不是'='... – 2014-12-07 07:17:28

+1

你为什么要分别加载每一行?试试'data = json.load(json_data)'。 – 2014-12-07 07:17:56

+0

@BurhanKhalid我想解析每一行并存储主题,单独提问和摘录 – user3890141 2014-12-07 07:25:31

回答

2

你试过:

json.loads(line.decode("utf-8")) 

类似的问题在这里问:UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2

编辑: 如果上述方法无效,

json.loads(line.decode("utf-8","ignore")) 

意志。

+0

添加.decode(“utf-8”)后我得到同样的错误 – user3890141 2014-12-07 07:30:22

+0

.decode(“utf-8”,'ignore' ) – Academiphile 2014-12-07 07:32:47

+0

这似乎已允许加载文件! (Y) – user3890141 2014-12-07 07:43:38