将两个json文件合并为一个文件Python

Posted

技术标签:

【中文标题】将两个json文件合并为一个文件Python【英文标题】:Merge two json files into a single file Python 【发布时间】:2021-08-13 17:39:35 【问题描述】:

我正在调用分页 API 并将不同页面的数据存储到单独的文件中。下面添加了这两个文件。

file1.json


    "page": 1,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        "id": 1,
        "email": "george.bluth@reqres.in",
        "first_name": "George",
        "last_name": "Bluth",
        "avatar": "https://reqres.in/img/faces/1-image.jpg"
    , 
        "id": 2,
        "email": "janet.weaver@reqres.in",
        "first_name": "Janet",
        "last_name": "Weaver",
        "avatar": "https://reqres.in/img/faces/2-image.jpg"
    , 
        "id": 3,
        "email": "emma.wong@reqres.in",
        "first_name": "Emma",
        "last_name": "Wong",
        "avatar": "https://reqres.in/img/faces/3-image.jpg"
    , 
        "id": 4,
        "email": "eve.holt@reqres.in",
        "first_name": "Eve",
        "last_name": "Holt",
        "avatar": "https://reqres.in/img/faces/4-image.jpg"
    , 
        "id": 5,
        "email": "charles.morris@reqres.in",
        "first_name": "Charles",
        "last_name": "Morris",
        "avatar": "https://reqres.in/img/faces/5-image.jpg"
    , 
        "id": 6,
        "email": "tracey.ramos@reqres.in",
        "first_name": "Tracey",
        "last_name": "Ramos",
        "avatar": "https://reqres.in/img/faces/6-image.jpg"
    ]

文件2.json


    "page": 2,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        "id": 7,
        "email": "michael.lawson@reqres.in",
        "first_name": "Michael",
        "last_name": "Lawson",
        "avatar": "https://reqres.in/img/faces/7-image.jpg"
    , 
        "id": 8,
        "email": "lindsay.ferguson@reqres.in",
        "first_name": "Lindsay",
        "last_name": "Ferguson",
        "avatar": "https://reqres.in/img/faces/8-image.jpg"
    , 
        "id": 9,
        "email": "tobias.funke@reqres.in",
        "first_name": "Tobias",
        "last_name": "Funke",
        "avatar": "https://reqres.in/img/faces/9-image.jpg"
    , 
        "id": 10,
        "email": "byron.fields@reqres.in",
        "first_name": "Byron",
        "last_name": "Fields",
        "avatar": "https://reqres.in/img/faces/10-image.jpg"
    , 
        "id": 11,
        "email": "george.edwards@reqres.in",
        "first_name": "George",
        "last_name": "Edwards",
        "avatar": "https://reqres.in/img/faces/11-image.jpg"
    , 
        "id": 12,
        "email": "rachel.howell@reqres.in",
        "first_name": "Rachel",
        "last_name": "Howell",
        "avatar": "https://reqres.in/img/faces/12-image.jpg"
    ]

我想合并这两个文件,特别是数据字段,以便结果显示如下 输出.json


    "page": 1,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        "id": 1,
        "email": "george.bluth@reqres.in",
        "first_name": "George",
        "last_name": "Bluth",
        "avatar": "https://reqres.in/img/faces/1-image.jpg"
    , 
        "id": 2,
        "email": "janet.weaver@reqres.in",
        "first_name": "Janet",
        "last_name": "Weaver",
        "avatar": "https://reqres.in/img/faces/2-image.jpg"
    , 
        "id": 3,
        "email": "emma.wong@reqres.in",
        "first_name": "Emma",
        "last_name": "Wong",
        "avatar": "https://reqres.in/img/faces/3-image.jpg"
    , 
        "id": 4,
        "email": "eve.holt@reqres.in",
        "first_name": "Eve",
        "last_name": "Holt",
        "avatar": "https://reqres.in/img/faces/4-image.jpg"
    , 
        "id": 5,
        "email": "charles.morris@reqres.in",
        "first_name": "Charles",
        "last_name": "Morris",
        "avatar": "https://reqres.in/img/faces/5-image.jpg"
    , 
        "id": 6,
        "email": "tracey.ramos@reqres.in",
        "first_name": "Tracey",
        "last_name": "Ramos",
        "avatar": "https://reqres.in/img/faces/6-image.jpg"
    , 
        "id": 7,
        "email": "michael.lawson@reqres.in",
        "first_name": "Michael",
        "last_name": "Lawson",
        "avatar": "https://reqres.in/img/faces/7-image.jpg"
    , 
        "id": 8,
        "email": "lindsay.ferguson@reqres.in",
        "first_name": "Lindsay",
        "last_name": "Ferguson",
        "avatar": "https://reqres.in/img/faces/8-image.jpg"
    , 
        "id": 9,
        "email": "tobias.funke@reqres.in",
        "first_name": "Tobias",
        "last_name": "Funke",
        "avatar": "https://reqres.in/img/faces/9-image.jpg"
    , 
        "id": 10,
        "email": "byron.fields@reqres.in",
        "first_name": "Byron",
        "last_name": "Fields",
        "avatar": "https://reqres.in/img/faces/10-image.jpg"
    , 
        "id": 11,
        "email": "george.edwards@reqres.in",
        "first_name": "George",
        "last_name": "Edwards",
        "avatar": "https://reqres.in/img/faces/11-image.jpg"
    , 
        "id": 12,
        "email": "rachel.howell@reqres.in",
        "first_name": "Rachel",
        "last_name": "Howell",
        "avatar": "https://reqres.in/img/faces/12-image.jpg"
    ]

应该采用什么方法?这里没有规则。如果添加了更多文件,则数据字段中的数据应该继续追加。

【问题讨论】:

只需要追加数据字段吗? 是的。如您在示例中所见, output.json 文件将继续附加来自不同文件的数据字段。文件 1 包含来自 id 1-6 的数据,文件 2 包含来自 id 7-12 的数据,然后将其附加到输出文件中。 有没有规定不能添加重复ID? 【参考方案1】:

您可以使用extend 将一个列表扩展为另一个列表:

import json

with open('json1.json') as json_file:
    json1 = json.load(json_file)

with open('json2.json') as json_file:
    json2 = json.load(json_file)

json1['data'].extend(json2['data'])

如果你想单独存储它们,或者创建一个空列表,你可以将data 中的所有列表扩展到其中。

【讨论】:

【参考方案2】:

你可以试试这个:

import json

with open("file1.json") as fo:
    data1 = json.load(fo)

with open("file2.json") as fo:
    data2 = json.load(fo)

data1.update(data2)

with open("fileout.json", "w") as fo:
    json.dump(data1, fo)


【讨论】:

【参考方案3】:

试试这个方法,确保使用不同的文件处理对象..

import json
with open('file1.json') as f:
    data1 = json.load(f)

with open('file2.json') as f1:
    data2 = json.load(f1)

data1['data'].extend(data2['data']) # Modified

with open('output.json','w') as out:
    json.dump(data1,out)

更好的方法,

import glob
import json

def processFiles():
    Files = glob.glob('Files/*.json')
    for i,singleFile in enumerate(Files):
        if i==0:
            with open(singleFile) as f:
                data = json.load(f)
        else:
            with open(singleFile) as f1:
                data['data'].extend(json.load(f1)['data'])
    return data

 
with open('output.json','w') as out:
    json.dump(processFiles(),out)

【讨论】:

我只获取文件 2 的数据,而不是文件 1。我使用了不同的文件处理程序对象。 现在可以查看了吗? 还需要什么? 有什么方法可以使用for循环一次性指定多个文件,这样就不会每次都打开单个文件并读取它们? 如果需要我可以添加吗?

以上是关于将两个json文件合并为一个文件Python的主要内容,如果未能解决你的问题,请参考以下文章

如何将多个文件进行合并?

js将两个json合并成一个,有啥好的方法

怎么把几个Excel合并成一个文件?

一个用于合并pdf的简单Python脚本

可以将多个lib文件合并成一个lib文件吗

python 如何把多个文件内容合并到以一个文件