2014-06-06 58 views
0

我嘗試讀取這個ASCII文件與此json content具有以下功能:如何閱讀使用JSON內容的ASCII文件:ValueError異常

{ "directory": { "name": "/wiki", "files": { "file": [ { "name": "/wiki/a.txt", "digest": "97d37a2ff85fbe35e1bf8ad38934d8fb518a6a3fbeb9b0b9305ce98e992f9dd2 " }, 
    { "name": "/wiki/d.txt", "digest": "ef91ee1257c3faa49f86f343cfec66010e5810e99db9f42e88774f90cd5b95d9 " },] } } } 


def readJsonFile(path): 
    with open(path) as json_file: 
     json_data = json.load(json_file) 

    return json_data 

我得到這個error沒有JSON對象的可解碼:

ValueError: No JSON object could be decoded 

我試着用json.loads,我得到的錯誤:

TypeError: expected string or buffer 

我使用正確的功能嗎?

+0

無效JSON。在http://jsonlint.com/上查看,它會告訴你什麼是錯的。 –

回答

1

該數據不是有效的json(它有一個尾隨,)。

但它是一個有效的python文字;您可以使用ast.literal_eval代替:

import ast 

def readJsonFile(path): 
    with open(path) as json_file: 
     return ast.literal_eval(json_file.read())