我有一個(gè)包含如下條目的文本文件:[{ "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 }}]我想要每個(gè)城市的規(guī)則,我需要修改這個(gè) json。我用 python 嘗試了一些東西,但我沒(méi)有得到類(lèi)似的東西我需要一個(gè) pyhton 腳本將此輸入規(guī)則轉(zhuǎn)換為下面的模型。預(yù)期的結(jié)果是,我希望結(jié)果是這樣的:所以我需要每個(gè)城市給我一個(gè)像 json 里面那樣的規(guī)則。感謝大家的幫助。我需要每個(gè)國(guó)家/地區(qū)的每條規(guī)則......以留在該國(guó)家/地區(qū)的特定 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貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊
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)
添加回答
舉報(bào)
0/150
提交
取消