使用Python更改JSON文件上的多个键

2024-06-25 22:51:53 发布

您现在位置:Python中文网/ 问答频道 /正文

如果满足条件,我将尝试在JSON文件中更新一个新密钥。下面是我的python代码,它试图在JSON文件中进行多次更新。你知道吗

 #!/usr/bin/env python
 # Usage: update json file 
import json
import os

json_dir="/opt/rdm/adggeth/ADGG-ETH-02/20181008/"
json_dir_processed="/opt/rdm/adggeth/ADGG-ETH-02/20181008updated/"
for json_file in os.listdir(json_dir):
    if json_file.endswith(".json"):
        processed_json = "%s%s" % (json_dir_processed, json_file)
        json_file = json_dir + json_file
        print "Processing %s -> %s" % (json_file, processed_json)
        with open(json_file, 'r') as f:
            json_data = json.load(f)
            # replacement mapping
            update_map = {"grp_farmerreg/farmerdetails/farmermobile":"grp_farmerdts/hh_id",
                          "grp_farmerdts/hh_region":"grp_farmerdts/region",
                          "grp_farmerdts/hh_district":"grp_farmerdts/district",
                          "grp_farmerdts/hh_ward":"grp_farmerdts/ward",
                          "grp_farmerdts/hh_village":"grp_farmerdts/village"}

            diff_keys = update_map.keys() - json_data.keys()
            if not diff_keys:
                print("No Update to JSON keys")
            else:
                for k in diff_keys:
                    json_data[k] = json_data[update_map[k]]
        with open(processed_json, 'w') as f:
            f.write(json.dumps(json_data, indent=4))
    else:
        print "%s not a JSON file" % json_file

我尝试更新的JSON文件如下:

{
    ....
    "farmerregistrd": "1", 
    "grp_farmerdts/region": "5", 
    "datacollid": "0923678275", 
    "_status": "submitted_via_web", 
    "enumtype": "2", 
    "deviceid": "352948096845916", 
    "start_time": "2019-04-03T10:57:23.620+03", 
    "_uuid": "f1069eae-33f8-4850-a549-49fcde27f077", 
    "grp_farmerdts/village": "2852", 
    "_submitted_by": null, 
    "formhub/uuid": "42cb3fc351a74fd89702078160f849ca", 
    "grp_farmerdts/hh_id": "623", 
    "grp_farmerdts/ward": "136", 
    ...
    "_userform_id": "adggeth_ADGG-ETH-REG02-20181008", 
    "_id": 711097, 
    "grp_farmerdts/district": "31"
} 

运行以下python文件的预期输出如下

 {
        ....
        "farmerregistrd": "1", 
        "grp_farmerdts/hh_region": "5", 
        "datacollid": "0923678275", 
        "_status": "submitted_via_web", 
        "enumtype": "2", 
        "deviceid": "352948096845916", 
        "start_time": "2019-04-03T10:57:23.620+03", 
        "_uuid": "f1069eae-33f8-4850-a549-49fcde27f077", 
        "grp_farmerdts/hh_village": "2852", 
        "_submitted_by": null, 
        "formhub/uuid": "42cb3fc351a74fd89702078160f849ca", 
        "grp_farmerdts/hh_id": "623", 
        "grp_farmerdts/hh_ward": "136", 
        ...
        "_userform_id": "adggeth_ADGG-ETH-REG02-20181008", 
        "_id": 711097, 
        "grp_farmerdts/hh_district": "31"
    } 

Tags: 文件idjsondatahhdirupdatekeys
2条回答

根据您的预期输出,需要检查所有特定的键(而不是其中一个)。更改逻辑如下所示:

...
json_data = json.load(f)
# replacement mapping
update_map = {"grp_farmerreg/farmerdetails/farmermobile":"grp_farmerdts/hh_id",
              "grp_farmerdts/hh_region":"grp_farmerdts/region",
              "grp_farmerdts/hh_district":"grp_farmerdts/district",
              "grp_farmerdts/hh_ward":"grp_farmerdts/ward", "grp_farmerdts/hh_village":"grp_farmerdts/village"}

diff_keys = update_map.keys() - json_data.keys()
if not diff_keys:
    print("No Update to JSON keys")
else:
    for k in diff_keys:
        if update_map[k] in json_data:
            json_data[k] = json_data[update_map[k]]

使用re模块和带有object_hook=参数(doc)的json.loads()。此脚本将向每个grp_farmerdts/*键添加hh_前缀,其中不是:

json_str = '''{
    "farmerregistrd": "1",
    "grp_farmerdts/region": "5",
    "datacollid": "0923678275",
    "_status": "submitted_via_web",
    "enumtype": "2",
    "deviceid": "352948096845916",
    "start_time": "2019-04-03T10:57:23.620+03",
    "_uuid": "f1069eae-33f8-4850-a549-49fcde27f077",
    "grp_farmerdts/village": "2852",
    "_submitted_by": null,
    "formhub/uuid": "42cb3fc351a74fd89702078160f849ca",
    "grp_farmerdts/hh_id": "623",
    "grp_farmerdts/ward": "136",
    "_userform_id": "adggeth_ADGG-ETH-REG02-20181008",
    "_id": 711097,
    "grp_farmerdts/district": "31"
}'''

import re
import json

def change_keys(d):
    return {re.sub(r'grp_farmerdts/((?!hh_)(\w+))', r'grp_farmerdts/hh_\1', k): v for k, v in d.items()}

print(json.dumps(json.loads(json_str, object_hook=change_keys), indent=4))

印刷品:

{
    "farmerregistrd": "1",
    "grp_farmerdts/hh_region": "5",
    "datacollid": "0923678275",
    "_status": "submitted_via_web",
    "enumtype": "2",
    "deviceid": "352948096845916",
    "start_time": "2019-04-03T10:57:23.620+03",
    "_uuid": "f1069eae-33f8-4850-a549-49fcde27f077",
    "grp_farmerdts/hh_village": "2852",
    "_submitted_by": null,
    "formhub/uuid": "42cb3fc351a74fd89702078160f849ca",
    "grp_farmerdts/hh_id": "623",
    "grp_farmerdts/hh_ward": "136",
    "_userform_id": "adggeth_ADGG-ETH-REG02-20181008",
    "_id": 711097,
    "grp_farmerdts/hh_district": "31"
}

相关问题 更多 >