简体   繁体   English

如何使用 Python 将 json 文件中的数据拆分为两个 json 文件

[英]How to split data from a json file into two json files with Python

I have a json file with two sets of data and I'd like to split this one json file into two separate json file so that each file has one set of data.我有一个包含两组数据的 json 文件,我想将这个 json 文件拆分为两个单独的 json 文件,以便每个文件都有一组数据。 For example, the existing json file looks like this:例如,现有的 json 文件如下所示:

{ 
  "client_id" : {"0": "abc123", "1": "def456"},
  "client_name": {"0": "companyA", "1": "companyB"},
  "revenue": {"0": "54,786", "1": "62,754"},
  "rate" : {"0": "4", "1": "5"}
}

I'm trying to make two separate json files for entry "0" and "1" like this.我正在尝试为条目“0”和“1”制作两个单独的 json 文件,就像这样。

File 1文件 1

{ 
  "client_id" : "abc123",
  "client_name": "companyA",
  "revenue": "54,786", 
  "rate" : "4"
}

File 2文件 2

{ 
  "client_id" :  "def456",
  "client_name": "companyB",
  "revenue":  "62,754",
  "rate" :  "5"
}

I tried to do this using a for loop but couldn't make it work.我尝试使用 for 循环来执行此操作,但无法使其工作。 Does anyone know how to split json files in Python?有谁知道如何在 Python 中拆分 json 文件?

You can try:你可以试试:

import json

dct = {
    "client_id": {"0": "abc123", "1": "def456"},
    "client_name": {"0": "companyA", "1": "companyB"},
    "revenue": {"0": "54,786", "1": "62,754"},
    "rate": {"0": "4", "1": "5"},
}

tmp = {}
for k, v in dct.items():
    for kk, vv in v.items():
        tmp.setdefault(kk, {}).update({k: vv})

for i, v in enumerate(tmp.values(), 1):
    with open(f"File{i}.json", "w") as f_out:
        json.dump(v, f_out, indent=4)

This creates two files File1.json , File2.json :这将创建两个文件File1.jsonFile2.json

{
    "client_id": "abc123",
    "client_name": "companyA",
    "revenue": "54,786",
    "rate": "4"
}

and

{
    "client_id": "abc123",
    "client_name": "companyA",
    "revenue": "54,786",
    "rate": "4"
}

EDIT: To create output dictionary:编辑:创建输出字典:

dct = {
    "client_id": {"0": "abc123", "1": "def456"},
    "client_name": {"0": "companyA", "1": "companyB"},
    "revenue": {"0": "54,786", "1": "62,754"},
    "rate": {"0": "4", "1": "5"},
}

tmp = {}
for k, v in dct.items():
    for kk, vv in v.items():
        tmp.setdefault(kk, {}).update({k: vv})

out = {}
for i, v in enumerate(tmp.values(), 1):
    out[f"File{i}"] = v

print(out)

Prints:印刷:

{
    "File1": {
        "client_id": "abc123",
        "client_name": "companyA",
        "revenue": "54,786",
        "rate": "4",
    },
    "File2": {
        "client_id": "def456",
        "client_name": "companyB",
        "revenue": "62,754",
        "rate": "5",
    },
}

You can use the json package to read your json file and process it in a for loop您可以使用json包来读取您的 json 文件并在 for 循环中处理它

import json

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

# Contains the "0", "1", ...
list_of_new_dicts = data["client_id"].keys()
new_data = {}

for key, dico in data.items():
    for num, value in dico.items():
        new_data[num][key] = value

Your new data dictionnary should look like the following:您的新数据字典应如下所示:

{
  "0":{
    "client_id" : "abc123",
    "client_name": "companyA",
    "revenue": "54,786", 
    "rate" : "4"
  },
  "1":{ 
    "client_id" :  "def456",
    "client_name": "companyB",
    "revenue":  "62,754",
    "rate" :  "5"
  }
}

Then to save the file you can do something like:然后要保存文件,您可以执行以下操作:

with open('json_data_0.json', 'w') as outfile:
    json.dump(new_data["0"], outfile)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM