Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 访问文件中的多个词典-Python_Json_Python 3.x_Dictionary_Stream - Fatal编程技术网

Json 访问文件中的多个词典-Python

Json 访问文件中的多个词典-Python,json,python-3.x,dictionary,stream,Json,Python 3.x,Dictionary,Stream,我对Json文件非常陌生。我有一个包含多个json对象的json文件,如下所示: {"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes", "Code":[{"event1":"A","result":"1"},…]}

我对Json文件非常陌生。我有一个包含多个json对象的json文件,如下所示:

{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",
  "Code":[{"event1":"A","result":"1"},…]}
{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",
  "Code":[{"event1":"B","result":"1"},…]}
{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",
  "Code":[{"event1":"B","result":"0"},…]}
…
我想像解析流一样解析这些json对象。然而,对我来说,最终的游戏是创建事件1和结果的成对组合。像这样:

[AB,AB,BB],[11,10,10]

据我所知: dict的确切结构

我不知道的是:如何通过dict提取这些dict来执行此操作

我无法修改现有文件,因此不要告诉我添加“[]”和“,”

其他帮助:


我可能会遇到无法直接存储在内存中的文件,因此流解决方案更受欢迎。

最简单的方法是将文件流提供给自定义生成器,该生成器将预解析json对象。这可以通过一些状态变量来完成,这些状态变量在某种程度上天真地计算开放{和[-的数量,每次它达到零,就会生成一个包含完整JSON对象的字符串

从您提供的示例中,我无法理解您想要的最终目的。我假设您在代码中有其他DICT,并且您最终想要的是一对组合事件1,结果在最外层DICT的每个代码值中。如果不是这样,请自行更改代码

有序的dict足以存储所需的结果,如果需要,您可以检索单独的键和值列表

from collections import OrderedDict
import json
import string
import sys

def char_streamer(stream):
    for line in stream:
        for char in line:
            yield char

def json_source(stream):
    result = []
    curly_count = 0
    bracket_count = 0
    nonwhitespace_count = 0
    inside_string = False
    previous_is_escape = False
    for char in char_streamer(stream):
        if not result and char in string.whitespace:
            continue
        result.append(char)

        if char == '"':
            if inside_string:
                inside_string = True
            elif not previous_is_escape:
                inside_string = False

        if inside_string:
            if char == "\\": # single '\' character
                previous_is_escape = not previous_is_escape
            else:
                previous_is_escape = False

            continue

        if char == "{":
            curly_count += 1
        if char == "[":
            bracket_count += 1

        if char == "}":
            curly_count -= 1
        if char == "]":
            bracket_count -= 1

        if curly_count == 0 and bracket_count== 0 and result:
            yield(json.loads("".join(result)))
            result = []




def main(filename):
    result = OrderedDict()
    with open(filename) as file:
        for data_part in json_source(file):
            # agregate your data here

    print (result.keys(), result.values())

main(sys.argv[1])