[英]migrate from ET.parse to etree.iterparse
Wrote a code to parse .osm file. 编写代码以解析.osm文件。 Spend a lot of time to build a up to 50 rows code but ran into a 'Memory Error' problem.
花很多时间来构建多达50行的代码,但遇到了“内存错误”问题。 Seems like the best solution is to use interparse() instead of parse().
似乎最好的解决方案是使用interparse()而不是parse()。
My question is: how should I change my code (strating of my code) 我的问题是:我应该如何更改我的代码 (说明我的代码)
import xml.etree.ElementTree as ET
tree = ET.parse('file.osm')
root = tree.getroot()
to ( using interparse() method) (not my code) 到(使用interparse()方法) (不是我的代码)
import xml.etree.ElementTree as etree
context=etree.iterparse('file.osm', events=('start', 'end', 'start-ns', 'end-ns'))
and not to ruin rest of my code (only part of my code) 而不破坏我的其余代码 (仅部分代码)
list=[]
for i in root.findall('node'):
lat=i.get('lat')
lon=i.get('lon')
dict = {}
for ii in i:
dict['lat']=lat
dict['lon']=lon
key=ii.get('k')
val=ii.get('v')
dict[key]=val
if len(dict)>0:
list.append(dict)
tree = ET.iterparse('file.osm')
root = tree.root
this will give you the tree root. 这将为您提供树根。 from there it is the same as parse
从那里它与解析相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.