Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python字典格式?_Python_Dictionary - Fatal编程技术网

python字典格式?

python字典格式?,python,dictionary,Python,Dictionary,我正在尝试解析开放街道地图中的xml文件。 这是我试图清理数据的全部代码。 我要看的部分是xml的第二层,地址是一个嵌套的dict,比如josn。我想不出来。在我为第二级编写代码之后,我无法打印顶层 def shape_element(element): node = {} a=[] if element.tag == "node" or element.tag == "way" : data=element.attrib

我正在尝试解析开放街道地图中的xml文件。 这是我试图清理数据的全部代码。 我要看的部分是xml的第二层,地址是一个嵌套的dict,比如josn。我想不出来。在我为第二级编写代码之后,我无法打印顶层

def shape_element(element):
    node = {}
    a=[]               
    if element.tag == "node" or element.tag == "way" :
        data=element.attrib
        node["created"]={c:data[c] for c in CREATED}
        a.append(element.get("lat"))
        a.append(element.get("lon"))
        node["pos"]=a
        node["id"]=data["id"]
        node["visible"]=data["visible"]

    if element.tag =="tag":
        for tag in element.iter():
            atb=tag.attrib
            i=atb["k"]

            if re.search(lower_colon,i):
                node["address"]={}
                m=re.search("(?<=addr:)\w+",i)
                k=m.group(0)
                node["address"][k]=atb['v']
        pprint.pprint (node)
def process_map(file_in, pretty = False):
file_out = "{0}.json".format(file_in)
data = []
with codecs.open(file_out, "w") as fo:
    for _, element in ET.iterparse(file_in):
        el = shape_element(element)
        if el:
            data.append(el)
            if pretty:
                fo.write(json.dumps(el, indent=2)+"\n")
            else:
                fo.write(json.dumps(el) + "\n")
return data
但我期待的是:

"address": {
              "housenumber": "5157",
              "postcode": "60625",
              "street": "North Lincoln Ave"
           }
我希望我的最终结果如下:

{
"id": "2406124091",
"type: "node",
"visible":"true",
"created": {
          "version":"2",
          "changeset":"17206049",
          "timestamp":"2013-08-03T16:43:42Z",
          "user":"linuxUser16",
          "uid":"1219059"
        },
"pos": [41.9757030, -87.6921867],
"address": {
          "housenumber": "5157",
          "postcode": "60625",
          "street": "North Lincoln Ave"
         },
"amenity": "restaurant",
"cuisine": "mexican",
"name": "La Cabana De Don Luis",
"phone": "1 (773)-271-5176"
}

您编写了什么代码来生成该dict?我正在使用元素树解析开放街道地图中的xml文件<代码>if element.tag==“tag”:for element.iter()中的tag:atb=tag.attrib i=atb[“k”]if re.search(下冒号,i):node[“address”]={}m=re.search((?@kartikelangovan在您的问题中进行更改:)您还可以显示
节点
的来源和打印输出的位置吗?请尝试将
节点[“地址”]={}
写入此if语句下方
if element.tag==“tag”:
{
"id": "2406124091",
"type: "node",
"visible":"true",
"created": {
          "version":"2",
          "changeset":"17206049",
          "timestamp":"2013-08-03T16:43:42Z",
          "user":"linuxUser16",
          "uid":"1219059"
        },
"pos": [41.9757030, -87.6921867],
"address": {
          "housenumber": "5157",
          "postcode": "60625",
          "street": "North Lincoln Ave"
         },
"amenity": "restaurant",
"cuisine": "mexican",
"name": "La Cabana De Don Luis",
"phone": "1 (773)-271-5176"
}