【问题标题】:Parse a file with JSON objects in Python在 Python 中解析带有 JSON 对象的文件
【发布时间】:2018-08-01 11:10:33
【问题描述】:

我有一个具有这种结构的文件:

{ 
  "key" : "A",
  "description" : "1",
  "uninterestingInformation" : "whatever"

}
{ 
  "key" : "B",
  "description" : "2",
  "uninterestingInformation" : "whatever"

}
{ 
  "key" : "C",
  "description" : "3",
  "uninterestingInformation" : "whatever"

}

我想在 Python 中构建一个字典,其中包含作为键的键和作为值的描述。我有更多的领域,但只有其中 2 个是我感兴趣的。

这个文件并不完全是一个 .json 文件,它是一个包含很多类似 json 对象的文件。

json.loads 显然不起作用。

关于如何读取数据有什么建议吗?

我已经看过this post,但是我的json对象不在一行...

编辑:

如果我的解释不清楚,这个例子是相当准确的,我有很多类似的JSON对象,一个接一个,用换行符(\n)分隔,没有逗号。因此,总体而言,该文件不是有效的 JSON 文件,而每个对象都是有效的 JSON 对象。

我最终应用的解决方案是:

api_key_file = open('mongo-config.json').read()
api_key_file = '[' + api_key_file + ']'
api_key_file= api_key_file.replace("}\n{", "},\n{")
api_key_data = json.loads(api_key_file)
api_key_description = {}
for data in api_key_data:
    api_key_description[data['apiKey']] = data['description']

它很适合我的情况。下面的 cmets 解释了可能有更好的方法来做到这一点。

【问题讨论】:

    标签: python json


    【解决方案1】:

    另一种选择是使用ast 模块中的literal_eval 函数,在进行必要的更改以使其符合有效类型的格式后:

    from ast import literal_eval
    
    inJson = '''{ 
      "key" : "A"
      "description" : "1"
      "uninterestingInformation" : "whatever"
    
    }
    { 
      "key" : "B"
      "description" : "2"
      "uninterestingInformation" : "whatever"
    
    }
    { 
      "key" : "C"
      "description" : "3"
      "uninterestingInformation" : "whatever"
    
    }'''
    
    inJson = "[" + inJson.replace("}", "},")[:-1] + "]"
    inJson = inJson.replace("\"\n  ","\",")
    
    
    newObject = literal_eval(inJson)
    print(newObject)
    

    输出:

    [{'key': 'A', 'description': '1', 'uninterestingInformation': 'whatever'}, {'key': 'B', 'description': '2', 'uninterestingInformation': 'whatever'}, {'key': 'C', 'description': '3', 'uninterestingInformation': 'whatever'}]
    

    【讨论】:

    • 最后,我做了类似的事情。我用 '},/n{' 替换了 '}/n{'。
    【解决方案2】:

    您可以使用re.split将文件内容拆分成合适的JSON字符串进行解析:

    import re
    import json
    j='''{ 
      "key" : "A",
      "description" : "1",
      "uninterestingInformation" : "whatever"
    
    }
    { 
      "key" : "B",
      "description" : "2",
      "uninterestingInformation" : "whatever"
    
    }
    { 
      "key" : "C",
      "description" : "3",
      "uninterestingInformation" : "whatever"
    
    }'''
    print(list(map(json.loads, re.split(r'(?<=})\n(?={)', j))))
    

    这个输出:

    [{'key': 'A', 'description': '1', 'uninterestingInformation': 'whatever'}, {'key': 'B', 'description': '2', 'uninterestingInformation': 'whatever'}, {'key': 'C', 'description': '3', 'uninterestingInformation': 'whatever'}]
    

    【讨论】:

    • 你已经输入了逗号。那不是 OP 拥有的数据,它们是换行符分隔的。
    • 我的假设基于 OP 的声明“这个文件不完全是 .json 文件,是一个包含很多类似 json 对象的文件。”,这意味着该文件有几个有效的 JSON 对象,而他的示例中缺少逗号只是因为他的快速输入仅用于说明目的。
    • 这是一个糟糕的假设。我想说假设 OP 的例子是准确的要好得多。
    • OP 的示例使用了虚假的键和值,因此很明显他是手动键入所有内容的,因此他对文件的描述应该比他键入的结构更认真。
    • 这个例子是正确的。可能我解释得不好。我有很多JSON对象一个接一个,但没有放在一个列表中,所以整个文件不是一个有效的JSON文件,但每个分开的对象都是一个正确的JSON对象。我想我下次应该尝试更清楚。 OP是一个女孩,顺便说一句:D但没关系。
    猜你喜欢
    • 1970-01-01
    • 2019-09-03
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多