2017-09-16 15 views
0

每一行都是有效的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 
+1

是否每行都有效JSON?例如:'行= [json.loads(line)inline in inpt]'做这项工作? –

+1

'lines.append(json.loads(line))'? –

+0

是的,但我不想处理每一行 - 我想整个处理文件 - 真实的文件有数百万条记录 –

回答

2

这看起来像我最近一直在使用的NDJSON。规范是here,我不确定它的用处。以下工作?

with open('the file.json', 'rb') as infile: 
    data = infile.readlines() 
    data = [json.loads(item.replace('\n', '')) for item in data] 

这应该给你一个词典列表。

+0

当我刚刚尝试出来我得到这个错误'print(data ['record']) TypeError:列表索引必须是整数,而不是str',我如何验证这个工作? –

+0

因为这会解析文件并给你一个字典列表,而不是字典。 – roganjosh

+0

但我想像json一样与json进行交互,在普通json中,我可以调用像'data ['record']'你知道我的意思吗? –

2

每一行看起来像一个有效的JSON文件。

这是 “JSON行” 格式(http://jsonlines.org/

尝试independantly处理每行(json.loads(line)),或使用专门的库(https://jsonlines.readthedocs.io/en/latest/)。

def process(oneline): 
    # do what you want with each line 
    print(oneline['record']) 

with open('toy_two.json', 'rb') as inpt: 
    for line in inpt: 
     process(json.loads(line)) 
+0

我想整个处理文件 - 因为真正的文件有数百万条记录 –

+0

那么?您可以像在代码中一样迭代输入文件的每一行,然后在'for'循环中应用json.loads(line)。 –

+0

听起来很贵,我想要廉价而快速地做到这一点 –