<GeocodeResponse>
<status>OK</status>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>Chengam, Tamil Nadu 606701, India</formatted_address>
<address_component>
<long_name>Chengam</long_name>
<short_name>Chengam</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Tiruvannamalai</long_name>
<short_name>Tiruvannamalai</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Tamil Nadu</long_name>
<short_name>TN</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>606701</long_name>
<short_name>606701</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>12.3067864</lat>
<lng>78.7957856</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>12.2982423</lat>
<lng>78.7832165</lng>
</southwest>
<northeast>
<lat>12.3213030</lat>
<lng>78.8035583</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>12.2982423</lat>
<lng>78.7832165</lng>
</southwest>
<northeast>
<lat>12.3213030</lat>
<lng>78.8035583</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJu8JCb3jxrDsRAOfhACQczWo</place_id>
</result>
</GeocodeResponse>
我是xml的新手,我不知道如何用python xml.etree处理它?我从https://docs.python.org/2/library/xml.etree.elementtree.html#parsing-xml读取的基本内容非常有用,但仍然难以打印出几何下的纬度和经度值 - &gt; location.i尝试了类似这样的内容
with open('data.xml', 'w') as f:
f.write(xmlURL.text)
tree = ET.parse('data.xml')
root = tree.getroot()
lat = root.find(".//geometry/location")
print(lat.text)
答案 0 :(得分:1)
你几乎得到了它。将root.find(".//geometry/location")
更改为root.find(".//geometry/location/lat")
:
lat = root.find(".//geometry/location/lat")
print(lat.text)
>> 12.3067864
同样适用于lng
:
lng = root.find(".//geometry/location/lng")
print(lng.text)
>> 78.7957856