<?xml version="1.0"?><menu><breakfast hours="7-11"><item price="$6.00">breakfast burritos</item><item price="$4.00">pancakes</item></breakfast><lunch hours="11-3"><item price="$5.00">hamburger</item></lunch><dinner hours="3-10"><item price="8.00">spaghetti</item></dinner></menu>import xml.etree.ElementTree as ettree = et.ElementTree(file='menu.xml')root = tree.getroot()root.tag#tag是标签字符串,attrib是属性的一个字典for child in root:print('tag:', child.tag, 'attributes:', child.attrib)for grandchild in child:print('\ttag:', grandchild.tag, 'attributes:', grandchild.attrib)len(root) #菜单选择数目len(roo[0]) #早餐项的数目原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。