我有一个包含如下条目的文本文件:[{ "Exceptions": { "GUID": "028326A8206D44A736B45FE6125E59B2", "ExceptionName": "madrid,barcelona", "TargetHostname": "www.spain.com", "TargetName": "spain", "HasModule": true }},{ "Exceptions": { "GUID": "028326A8206D44A736B45FE6125E59B2", "ExceptionName": "london,liverpool", "TargetHostname": "www.uk.com", "TargetName": "uk", "HasModule": true }},{ "Exceptions": { "GUID": "028326A8206D44A736B45FE6125E59B2", "ExceptionName": "dublin,cork", "TargetHostname": "www.ireland.com", "TargetName": "ireland", "HasModule": true }}]我想要每个城市的规则,我需要修改这个 json。我用 python 尝试了一些东西,但我没有得到类似的东西我需要一个 pyhton 脚本将此输入规则转换为下面的模型。预期的结果是,我希望结果是这样的:所以我需要每个城市给我一个像 json 里面那样的规则。感谢大家的帮助。我需要每个国家/地区的每条规则......以留在该国家/地区的特定 json 中。示例 spain.json[{"Exceptions": { "GUID": "028326A8206D44A736B45FE6125E59B2", "ExceptionName": "madrid", "TargetHostname": "www.spain.com", "TargetName": "spain", "HasModule": true } }, {"Exceptions": { "GUID": "028326A8206D44A736B45FE6125E59B2", "ExceptionName": "barcelona", "TargetHostname": "www.spain.com", "TargetName": "spain", "HasModule": true } }]例子 uk.json[{"Exceptions": { "GUID": "028326A8206D44A736B45FE6125E59B2", "ExceptionName": "london", "TargetHostname": "www.uk.com", "TargetName": "uk", "HasModule": true } }, {"Exceptions": { "GUID": "028326A8206D44A736B45FE6125E59B2", "ExceptionName": "liverpool", "TargetHostname": "www.uk.com", "TargetName": "uk", "HasModule": true } }]你可以帮我。
1 回答
慕桂英4014372
TA贡献1871条经验 获得超13个赞
import copy
import json
mylist = []
for e in a: #a is the original list.
mylist = []
for city in e["Exceptions"]["ExceptionName"].split(","):
new = copy.deepcopy(e)
new["Exceptions"]["ExceptionName"] = city
mylist.append(new)
with open(f"{new['Exceptions']['TargetName']}.json", "w") as f:
json.dump(mylist, f)
添加回答
举报
0/150
提交
取消