我试图从打开的街道地图解析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': {'city': 'Chicago'}}
{'address': {'housenumber': '5157'}}
{'address': {'postcode': '60625'}}
{'address': {'street': 'North Lincoln Ave'}}
但是我期待像
这样的东西"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"
}