我有一个具有这种结构的文件:
{
"key" : "A",
"description" : "1",
"uninterestingInformation" : "whatever"
}
{
"key" : "B",
"description" : "2",
"uninterestingInformation" : "whatever"
}
{
"key" : "C",
"description" : "3",
"uninterestingInformation" : "whatever"
}
我想用Python构建一个字典,其中包含键作为键,描述作为值。我还有更多字段,但是其中只有两个对我来说很有趣。
此文件并非完全是.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']
在我的情况下效果很好。在下面的评论中,可能有解决此问题的更好方法。
答案 0 :(得分:1)
另一种选择是在进行必要的更改以使其适合有效类型的格式之后,使用literal_eval
模块中的ast
函数:
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'}]
答案 1 :(得分:-1)
您可以使用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'}]