簡體   English   中英

使用 xml.etree.ElementTree 讀取 pandas、python 中的 a.osm 文件

[英]reading a .osm file in pandas, python using xml.etree.ElementTree

您好我正在嘗試使用xml.etree.ElementTree讀取pandas中的.osm文件。 如何獲取node內的idtag k = "" v = ""的值

輸入

<osm version="0.6" generator="CGImap 0.8.6 (895960 spike-06.openstreetmap.org)" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
 <bounds minlat="12.5770000" minlon="-2.9532000" maxlat="12.6328000" maxlon="-2.8514000"/>
 <node id="902955577" visible="true" version="6" changeset="90170478" timestamp="2020-08-31T06:29:59Z" user="pizzaiolo" uid="1772368" lat="12.6049118" lon="-2.9023135">
  <tag k="admin_level" v="8"/>
  <tag k="capital" v="8"/>
  <tag k="description" v="Commune rurale"/>
  <tag k="is_in" v="Toma;Nayala;Boucle du Mouhoun;Burkina Faso;Africa"/>
  <tag k="is_in:continent" v="Africa"/>
  <tag k="is_in:country" v="Burkina Faso"/>
  <tag k="is_in:country_code" v="BF"/>
  <tag k="is_in:department" v="Toma"/>
  <tag k="is_in:province" v="Nayala"/>
  <tag k="is_in:province_code" v="40"/>
  <tag k="is_in:region" v="Boucle du Mouhoun"/>
  <tag k="is_in:region_code" v="02"/>
  <tag k="name" v="Goa"/>
  <tag k="name:fr" v="Goa"/>
  <tag k="note" v="Inserted by script"/>
  <tag k="place" v="village"/>
  <tag k="population" v="651"/>
  <tag k="source" v="NGA GEOnet Names Server (GNS)"/>
  <tag k="source:population" v="INSD.BF"/>
  <tag k="wikidata" v="Q13414957"/>
 </node>

代碼

from xml.etree.ElementTree import parse
import pandas as pd

mydata = parse('map.osm')
print(mydata,'\n')

my_id=[]

for item in mydata.iterfind('node'):
    my_id.append(item.findtext('id'))
    
print(my_id)

Output : <xml.etree.ElementTree.ElementTree object at 0x0000018504B10160>

[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

findtext查找元素,但id不是元素; 這是一個屬性。 您需要item.attrib['id']item.get('id')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM