gpt4 book ai didi

python - 如何合并 2 个 JSON 文件?

转载 作者:行者123 更新时间:2023-11-29 12:58:35 26 4
gpt4 key购买 nike

有两个不同的 JSON 文件。

[{"volume": "44", 
"affiliations": {},
"cite_count": 39,
"issue": "12",
"page_range": "1257-1271",
"doi": "10.1016/0584-8547(89)80124-7",
"title_en": "test"}
]

[{"sourceType": "Conference Proceeding",
"page_range": "1257-1271",
"language": null,
"volume": null,
"titleEn": "test2",
"spinCiteCount": null}
]

如您所见,它们具有相同的信息,但形式不同。有些信息不在其他文件中。我需要将它们全部转换成一个 postgreSQL 表(或 JSON 文件并解析它)。

那么,如何用Python将不同格式的JSON文件合并到一个文件或一张postgres表中呢?

最佳答案

您可以将两者合并到一个字典/json 中:

import json

dic1 = json.load('json_file_1')
dic2 = json.load('json_file_2')
dic1.update(dic2)

print dic1

输出:

{
"volume": "44",
"affiliations": {},
"cite_count": 39,
"issue": "12",
"page_range": "1257-1271",
"doi": "10.1016/0584-8547(89)80124-7",
"title_en": "test2"
"sourceType": "Conference Proceeding",
"language": None,
"volume": None,
"spinCiteCount": None
}

Note: the common keys will be overwritten by the values of the second file. So the order you read the files matters, based on what you need, change the order.

关于python - 如何合并 2 个 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com