如何把复合字典中的城市代码提取出来
如何把下面字典中的城市代码提取出来(代码太长,我只放了前面一小部分上来){
"flag": 1,
"data": {
"data": {
"country": {
"list": {
"1": {
"enName": "hot country",
"name": "热门国家"
},
"350": {
"code": "350",
"abroadFlag": "2",
"level": "1",
"spelling": "YZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "350",
"seoUri": "asia",
"pinyin": "YaZhou",
"parentCode": "",
"enName": "Asia",
"name": "亚洲",
"category": 0
},
"360": {
"code": "360",
"abroadFlag": "2",
"level": "1",
"spelling": "BMZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "360",
"seoUri": "northamerica",
"pinyin": "BeiMeiZhou",
"parentCode": "",
"enName": "NorthAmerica",
"name": "北美洲",
"category": 0
},
"370": {
"code": "370",
"abroadFlag": "2",
"level": "1",
"spelling": "NMZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "370",
"seoUri": "southamerica",
"pinyin": "NanMeiZhou",
"parentCode": "",
"enName": "SouthAmerica",
"name": "南美洲",
"category": 0
},
"380": {
"code": "380",
"abroadFlag": "2",
"level": "1",
"spelling": "DYZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "380",
"seoUri": "oceania",
"pinyin": "DaYangZhou",
"parentCode": "",
"enName": "Oceania",
"name": "大洋洲",
"category": 0
},
"390": {
"code": "390",
"abroadFlag": "2",
"level": "1",
"spelling": "OZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "390",
"seoUri": "europe",
"pinyin": "OuZhou",
"parentCode": "",
"enName": "Europe",
"name": "欧洲",
"category": 0
},
"400": {
"code": "400",
"abroadFlag": "2",
"level": "1",
"spelling": "FZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "400",
"seoUri": "africa",
"pinyin": "FeiZhou",
"parentCode": "",
"enName": "Africa",
"name": "非洲",
"category": 0
},
"410": {
"code": "410",
"pinyin": "ZhongGuo",
"abroadFlag": "0",
"level": "2",
"parentCode": "350",
"spelling": "ZG",
"areaNumber": "",
"enName": "China",
"name": "中国",
"sort": "410",
"category": 1,
"seoUri": "china"
},
"version": 20220406
}
}
应该是 json 数据,最简单粗暴直接一层层拨开 for 循环嘛,参考代码:
temp = {
"flag": 1,
"data": {
"data": {
"country": {
"list": {
"1": {
"enName": "hot country",
"name": "热门国家"
},
"350": {
"code": "350",
"abroadFlag": "2",
"level": "1",
"spelling": "YZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "350",
"seoUri": "asia",
"pinyin": "YaZhou",
"parentCode": "",
"enName": "Asia",
"name": "亚洲",
"category": 0
},
"360": {
"code": "360",
"abroadFlag": "2",
"level": "1",
"spelling": "BMZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "360",
"seoUri": "northamerica",
"pinyin": "BeiMeiZhou",
"parentCode": "",
"enName": "NorthAmerica",
"name": "北美洲",
"category": 0
},
"370": {
"code": "370",
"abroadFlag": "2",
"level": "1",
"spelling": "NMZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "370",
"seoUri": "southamerica",
"pinyin": "NanMeiZhou",
"parentCode": "",
"enName": "SouthAmerica",
"name": "南美洲",
"category": 0
},
"380": {
"code": "380",
"abroadFlag": "2",
"level": "1",
"spelling": "DYZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "380",
"seoUri": "oceania",
"pinyin": "DaYangZhou",
"parentCode": "",
"enName": "Oceania",
"name": "大洋洲",
"category": 0
},
"390": {
"code": "390",
"abroadFlag": "2",
"level": "1",
"spelling": "OZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "390",
"seoUri": "europe",
"pinyin": "OuZhou",
"parentCode": "",
"enName": "Europe",
"name": "欧洲",
"category": 0
},
"400": {
"code": "400",
"abroadFlag": "2",
"level": "1",
"spelling": "FZ",
"cityLevel": 999,
"areaNumber": "",
"sort": "400",
"seoUri": "africa",
"pinyin": "FeiZhou",
"parentCode": "",
"enName": "Africa",
"name": "非洲",
"category": 0
},
"410": {
"code": "410",
"pinyin": "ZhongGuo",
"abroadFlag": "0",
"level": "2",
"parentCode": "350",
"spelling": "ZG",
"areaNumber": "",
"enName": "China",
"name": "中国",
"sort": "410",
"category": 1,
"seoUri": "china"
}
}
}
}
}
}
data = temp["data"]["data"]["country"]["list"]
for i in data:
if i == "1":
continue
print(data["code"], data["name"])
你的数据删减的有问题,括号都少了,另外我把 version 的那个键值对给删了
递归解析吧 正则
页:
[1]