如何读取包含多个json和定界符的文本文件是Python中带空格的新行 [英] How to read text file which contains multiple json and delimiter used is new line with space in Python

查看:122
本文介绍了如何读取包含多个json和定界符的文本文件是Python中带空格的新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中存在多个JSON

I have a file where multiple JSON are present

{
  "schema": "2.0",
  "comp": [
    "fid1"
  ],
  "name": "Temp1",
  "type": "type1",
  "attr": {
    "version": "10.2.0.3"
  }
}

{
  "time": "18:21:58",
  "process": "Start",
  "msg": "Start"
}

我想将其解析为多个JSON对象.我尝试使用json.load,但由于它不是纯json文件,因此无法正常工作.其他选项是:

I want to parse this into multiple JSON objects. I tried to use json.load but since it is not a pure json file it does not work. Other options are :

  • 像字符串一样阅读,并以开始和结束括号为基础,捕获JSON.这种方法的缺点是,如果文件太大,它将变得很复杂
  • 根据新的线条和空间分割.

还有其他解析方法,即使文件大小增加,该方法也可以适应吗?此外,文件中的JSON可以不同.

Is there any other way to parse and that can adapt even if file size grows? Also, JSON within the file can be different.

推荐答案

将其作为字符串处理并使用堆栈保存"{"(不能用于键或包含单个{}\w*{):

Handle it as a string and use a stack to save the "{"(Couldn't be used to key or value contains single {,} or }\w*{):

import json
# use open() function to open your text file.
my_json = ''' 
{
  "schema": "2.0",
  "comp": [
    "fid1"
  ],
  "name": "Temp1",
  "type": "type1",
  "attr": {
    "version": "10.2.0.3"
  }
}

{
  "time": "18:21:58",
  "process": "Start",
  "msg": "Start"
}
'''
stack = []
jsonstr = ""
json_list = []
for i in range(len(my_json)):
    if my_json[i] == '{':
        stack.append("{")
    jsonstr += my_json[i]
    if my_json[i] == '}':
        stack.pop()
        if not stack: # if stack is empty
            # now you can write it in a file

            # with open("json_{}.json".format(index),'w+') as f:
            #     f.write(jsonstr.strip())

            # convert it to a json object
            jsonList.append(json.loads(jsonstr))
            jsonstr = ""

for i in jsonList:
    print(i)

结果:

{'schema': '2.0', 'comp': ['fid1'], 'name': 'Temp1', 'type': 'type1', 'attr': {'version': '10.2.0.3'}}
{'time': '18:21:58', 'process': 'Start', 'msg': 'Start'}

这篇关于如何读取包含多个json和定界符的文本文件是Python中带空格的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆