我正在循环一些XML文件并生成我想要以defaultdict(list)类型存储的树.每个循环和下一个找到的子项将存储在字典的单独部分中.
d = defaultdict(list)
counter = 0
for child in root.findall(something):
tree = ET.ElementTree(something)
d[int(x)].append(tree)
counter += 1
因此,对多个文件重复此操作将导致精确索引的结果;跨越不同解析文件的位置1的一组树,依此类推.问题是,我如何加入所有d,并将树(作为累积树)写入文件?
我可以遍历dict来获取每棵树:
for x in d:
for y in d[x]:
print (y)
这给出了我的词典中的完整树木列表.现在,我如何从中生产出一棵大树?
最佳答案
使用Spyne:
原文链接:https://www.f2er.com/python/439639.htmlfrom spyne.model.primitive import *
from spyne.model.complex import *
class GpsInfo(ComplexModel):
UTC = DateTime
Latitude = Double
Longitude = Double
DopplerTime = Double
Quality = Unicode
HDOP = Unicode
Altitude = Double
Speed = Double
Heading = Double
Estimated = Boolean
class Header(ComplexModel):
Name = Unicode
Time = DateTime
SeqNo = Integer
class CTrailData(ComplexModel):
index = UnsignedInteger
gpsInfo = GpsInfo
Header = Header
class CTrail(ComplexModel):
LastError = AnyXml
MaxTrial = Integer
Trail = Array(CTrailData)
from lxml import etree
from spyne.util.xml import *
file_1 = get_xml_as_object(etree.fromstring(open('file1').read()),CTrail)
file_2 = get_xml_as_object(etree.fromstring(open('file2').read()),CTrail)
file_1.Trail.extend(file_2.Trail)
file_1.Trail.sort(key=lambda x: x.index)
elt = get_object_as_xml(file_1,no_namespace=True)
print etree.tostring(elt,pretty_print=True)
在执行此操作时,Spyne还将数据字段从字符串转换为其本机Python格式,因此您可以更轻松地处理此xml文档中的数据.
另外,如果您不介意使用git的最新版本,您可以这样做:
class GpsInfo(ComplexModel):
# (...)
doppler_time = Double(sub_name="DopplerTime")
# (...)