Python读取xml文件使用Python语言及其应用一书的坑

晚上学习Python读取XML文件,在本书的162页,代码如下:


第二行对于文件的读取,使用Pycharm新建的XML文件是不自带.XML后缀名的,编译就会一直报错。

修改代码如下:

import xml.etree.ElementTree as ET
tree = ET.ElementTree(file='test')
root = tree.getroot()
print(root.tag)
for child in root:
    print('tag:' , child.tag , 'attributes:' , child.attrib)
    for grandchild in child:
        print('\ttag:' , grandchild.tag , 'attributes:' , grandchild.attrib)

猜你喜欢

转载自blog.csdn.net/u010760034/article/details/79560060