gpt4 book ai didi

python - 使用 Python 更改 JSON FILe 上的多个键

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:16 25 4
gpt4 key购买 nike

如果满足条件,我正在尝试将新 key 更新到我的 JSON 文件中。以下是我尝试在 JSON 文件中进行多次更新的 Python 代码。

 #!/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"
}

最佳答案

使用 re 模块和带有 object_hook= 参数的 json.loads() ( doc )。此脚本将为每个 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"
}

关于python - 使用 Python 更改 JSON FILe 上的多个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159940/

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