在用如下的代码读取 xml 文件时,代码如下:
from lxml import objectify
path = 'd_file/2.xml'
parsed = objectify.parse(open(path))
root = parsed.getroot()
data = []
skip_fields = ['PARENT_SEQ', 'INDICATOR_SEQ',
'DESIRED_CHANGE', 'DECIMAL_PLACES']
for elt in root.INDICATOR:
el_data = {}
for child in elt.getchildren():
if child.tag in skip_fields:
continue
el_data[child.tag] = child.pyval
data.append(el_data)
报错:
src\lxml\objectify.pyx in lxml.objectify.ObjectifiedElement.__getattr__()
src\lxml\objectify.pyx in lxml.objectify._lookupChildOrRaise()
AttributeError: no such child: INDICATOR
可以将代码中的 INDICATOR 删除,一样可以将json 转化为 DataFrame:
结果如下: