每一行都是有效的JSON,但是我需要将整个文件作为有效的JSON。在python中创建有效的json对象
我有一些数据是从一个Web服务聚合并转储到一个文件,所以它是JSON-eaque,但不是有效的JSON,因此无法以简单直观的方式处理JSON文件 - 从而consituting一个重大的痛苦在脖子,它看起来像这样(或多或少):
{"record":"value0","block":"0x79"}
{"record":"value1","block":"0x80"}
我一直在试图重新解释它作为有效的JSON,我的最新尝试是这样的:
with open('toy.json') as inpt:
lines = []
for line in inpt:
if line.startswith('{'): # block starts
lines.append(line)
但是,正如您可能因为我提出这个问题 - 这不起作用 - 我有什么想法可以解决这个问题?
编辑:
尝试这样:
with open('toy_two.json', 'rb') as inpt:
lines = [json.loads(line) for line in inpt]
print(lines['record'])
而且得到了以下错误:
Traceback (most recent call last):
File "json-ifier.py", line 38, in <module>
print(lines['record'])
TypeError: list indices must be integers, not str
理想情况下,我想,我可以用正常的JSON与它交互,即data['value']
编辑II
with open('transactions000000000029.json', 'rb') as inpt:
lines = [json.loads(line) for line in inpt]
for line in lines:
records = [item['hash'] for item in lines]
for item in records:
print item
是否每行都有效JSON?例如:'行= [json.loads(line)inline in inpt]'做这项工作? –
'lines.append(json.loads(line))'? –
是的,但我不想处理每一行 - 我想整个处理文件 - 真实的文件有数百万条记录 –