直接上代码
2019-03-06 本文已影响0人
空口言_1d2e
python 3.7 以后字典都是有序的
"""
获取行政规划代码
"""
from . import baidu_applet
from flask import jsonify
from .print_utils import divisions as _divisions
@baidu_applet.route("/divisions")
def divisions():
divisions_info = _divisions()
result=[{},{},{}]
for key,value in divisions_info.items():
if key.endswith("0000"):
result[0][key]=value
elif key.endswith("00"):
if key[:2] + "0000" in divisions_info:
result[1][key] = value
else:
result[0][key] = value
else:
if key[:2] + "0000" in divisions_info:
if key[:4] + "00" in divisions_info:
result[2][key] = value
else:
result[1][key] = value
else:
result[0][key] = value
result_province = []
for key,value in sorted(result[0].items()):
result_province.append({
"key": key,
"value": value
})
result_city = []
for key,value in sorted(result[1].items()):
result_city.append({
"key": key,
"value": value
})
result_district= []
for key,value in sorted(result[2].items()):
result_district.append({
"key": key,
"value": value
})
resp = jsonify({"code": 200, "description":[result_province,result_city,result_district]})
resp.headers["cache-control"] = "public, max-age=60"
return resp