- Python日常
内容展示: ①:读取xml中的节点及属性值
from xml.etree.ElementTree import parse # 导入库
doc = parse('files/products.xml')
print(type(doc))
for item in doc.iterfind('products/product'):
id = item.findtext('id')
name = item.findtext('name')
price = item.findtext('price')
uuid = item.get('uuid')
print('uuid', '=', uuid)
print('id', '=', id)
print('name', '=', name)
print('price', '=', price)
print('---------')
编写一个名为product.xml文档,位置放在files中
10000 iphone9 9999 20000 特斯拉 800000 30000 Mac pro 4000
总结:
通过parse函数可以读取xml文档,该函数返回elementTree类型对象,通过该对象的iterfind方法可以对xml中特定的节点进行迭代
- CSDN Python日常 第【4】1 篇
- 下一篇链接



