from xml.etree import ElementTree as ET
tree = ET.parse('data-text.xml')
#解析对象
root = tree.getroot()
#getroot方法获取根目录
data = root.find('Data')
#找到data节点
all_data= []
for observation in data:
record = {}
for item in observation:
lookup_key = list(item.attrib.keys())[0]
#attrib方法或得属性,key方法获得键,[0]索引获得第一个元素
if lookup_key== 'Numeric':
rec_key = 'Numeric'
rec_value=item.attrib['Numeric']
else:
rec_key=item.attrib[lookup_key]
rec_value=item.attrib['Code']
#attrib方法获得属性过后,有两种不同的字典,['category','code']和[0]
record[rec_key]=rec_value
all_data.append(record)
print(all_data)
python导入XML格式数据
最新推荐文章于 2024-07-04 04:29:55 发布