gpt4 book ai didi

python - 多个Dictionary JSON文件到pandas数据框

转载 作者:行者123 更新时间:2023-12-03 08:25:25 25 4
gpt4 key购买 nike

我有以下形式的在线数据:

{"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)

有人可以帮我了解我在这里缺少的重点吗?

最佳答案

这是因为文件中的中间一行。它不是有效的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': {}
}]

关于python - 多个Dictionary JSON文件到pandas数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43413692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com