2014-12-07 48 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