2017-04-14 53 views
0

我有以下形式的在線數據:多詞典JSON文件大熊貓數據幀

{"headers": {"ai5": "3356", "debug": null, "random": null, "sd": "7.6"}, "post": {"event": "ggstart", "ts": "1462"}, "params": {}, "bottle": {"timestamp": "2016-05-09 02:00:00.033775", "game_id": "55107008"}} 

{"headers": {"ai5": "8fa6", "debug": null, "random": null, "sd": "7.6"}, "post": {"event": "ggstart", "ts": "1475"}, "params": {}, "bottle": {"timestamp": "2016-05-09 02:00:00.004906", "game_id": "55107008"}} 

我期待,我會讀它考慮到各行作爲JSON格式,並繼續將其添加到最後數據:

data = [] 
with open('new.json') as f: 
    for line in f: 
     print(line) 
     data.append(json.loads(line)) 

但我收到錯誤:

JSONDecodeError: Expecting value: line 2 column 1 (char 1) 

能有人幫我在聯合國瞭解我在這裏失去了什麼重要的一點。

+0

它實際上是JSON在這一行? 'print(line)'的輸出是什麼? – Robert

回答

1

這是因爲你的文件中的中間行。它不是一個有效的json(實際上是一個空白行),因此您正面臨錯誤。

修復:

添加try/except塊。

import json 
data = [] 
with open('test.txt') as f: 
    for line in f: 
     try: 
      data.append(json.loads(line.strip())) 
     except ValueError: 
      pass 

print(data) 

輸出:

[{ 
'post': { 
    'event': 'ggstart', 
    'ts': '1462' 
}, 
'bottle': { 
    'timestamp': '2016-05-09 02:00:00.033775', 
    'game_id': '55107008' 
}, 
'headers': { 
    'debug': None, 
    'sd': '7.6', 
    'random': None, 
    'ai5': '3356' 
}, 
'params': {} 
}, 
{ 
    'post': { 
     'event': 'ggstart', 
     'ts': '1475' 
    }, 
    'bottle': { 
     'timestamp': '2016-05-09 02:00:00.004906', 
     'game_id': '55107008' 
    }, 
    'headers': { 
     'debug': None, 
     'sd': '7.6', 
     'random': None, 
     'ai5': '8fa6' 
    }, 
    'params': {} 
}]