将做工程过程经常用的代码做个珍藏,如下资料是关于将一个Etree XML结构转换为一个Python dict+list格式的的代码,应该能对各朋友有些用途。
from lxml import etree, objectify
def formatXML(parent):
"""
Recursive operation which returns a tree formated
as dicts and lists.
Decision to add a list is to find the 'List' word
in the actual parent tag.
"""
ret = {}
if parent.items(): ret.update(dict(parent.items()))
if parent.text: ret['__content__'] = parent.text
if ('List' in parent.tag):
ret['__list__'] = []
for element in parent:
if element.tag is not etree.Comment:
ret['__list__'].append(formatXML(element))
else:
for element in parent:
if element.tag is not etree.Comment:
ret[element.tag] = formatXML(element)
return ret