用python替换JSON文件中的密钥
我正在做一个简单的python脚本来替换JSON文件中的一个键,下面是我的python脚本用python替换JSON文件中的密钥,python,json,Python,Json,我正在做一个简单的python脚本来替换JSON文件中的一个键,下面是我的python脚本 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
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)
if "grp_farmerreg/farmerdetails/farmermobile" not in json_data:
json_data["grp_farmerreg/farmerdetails/farmermobile"] = json_data["grp_farmerdts/hh_id"]
with open(processed_json, 'w') as f:
f.write(json.dumps(json_data, indent=4))
else:
print "%s not a JSON file" % json_file
此脚本显示以下错误:
File "/opt/rdm/adggeth/ADGG-ETH-02/addfidkey.sh", line 16, in <module>
json_data["grp_farmerdts/hh_id"] = json_data["grp_farmerreg/farmerdetails/farmermobile"]
KeyError: 'grp_farmerreg/farmerdetails/farmermobile'
我的预期产出
{
"grp_farmerreg/members_no": "1",
"grp_farmerreg/hh_country": "1",
"_bamboo_dataset_id": "",
"_tags": [],
"grp_farmerreg/totanim": "6",
"gpsloc": "7.0854258 38.6111001 1674.300048828125 17.0",
"grp_farmerreg/farmerdetails/farmermobile": "0913886615",
"grp_farmerdts/hh_id":"0913886615",
"grp_farmerreg/grpdetails2/nmale6to14": "1",
"grp_farmerreg/farmerdetails/farmerhhhead": "1",
"grp_farmerreg/grpdetails2/nfem15to64": "0",
"meta/instanceID": "uuid:008aea99-810e-4dbc-9235-4d02b8e8d36b",
"_duration": "",
"grp_farmerreg/grpdetails2/nfem6to14": "1",
"grp_farmerreg/grp_e1_ctlca/cattletotalowned": "6",
"start_time": "2018-12-24T11:24:53.034+03",
"_uuid": "008aea99-810e-4dbc-9235-4d02b8e8d36b",
}
试着换一下台词 如果grp_farmerreg/farmerdetails/farmermobile不在json_数据中: json_数据[grp_farmerreg/farmerdetails/farmermobile]=json_数据[grp_farmerdts/hh_id] 与 如果json_数据中的grp_farmerreg/farmerdetails/farmermobile: json_数据[grp_farmerdts/hh_id]=json_数据[grp_farmerreg/farmerdetails/farmermobile] del json_数据[grp_farmerreg/farmerdetails/farmermobile]
我尝试过你的解决方案,但我得到了相同的错误@Julian MindeDid你是否也更改了我答案的第一行?Key错误表示json_数据中不存在Key grp_farmerdts/hh_id,这似乎是正确的。但是,通过分配它,您可以添加它,这应该很好。如果您尝试此解决方案,但出现密钥错误,则应提及另一个密钥。
{
"grp_farmerreg/members_no": "1",
"grp_farmerreg/hh_country": "1",
"_bamboo_dataset_id": "",
"_tags": [],
"grp_farmerreg/totanim": "6",
"gpsloc": "7.0854258 38.6111001 1674.300048828125 17.0",
"grp_farmerreg/farmerdetails/farmermobile": "0913886615",
"grp_farmerdts/hh_id":"0913886615",
"grp_farmerreg/grpdetails2/nmale6to14": "1",
"grp_farmerreg/farmerdetails/farmerhhhead": "1",
"grp_farmerreg/grpdetails2/nfem15to64": "0",
"meta/instanceID": "uuid:008aea99-810e-4dbc-9235-4d02b8e8d36b",
"_duration": "",
"grp_farmerreg/grpdetails2/nfem6to14": "1",
"grp_farmerreg/grp_e1_ctlca/cattletotalowned": "6",
"start_time": "2018-12-24T11:24:53.034+03",
"_uuid": "008aea99-810e-4dbc-9235-4d02b8e8d36b",
}