美文网首页
直接上代码

直接上代码

作者: 空口言_1d2e | 来源:发表于2019-03-06 19:40 被阅读0次

    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
    

    相关文章

      网友评论

          本文标题:直接上代码

          本文链接:https://www.haomeiwen.com/subject/syvxpqtx.html