【发布时间】:2021-08-09 15:44:13
【问题描述】:
以下代码在 Python 2 中运行良好。
import xml.etree.ElementTree as ET
import os
import json
file_name = 'sample.xml'
full_path = os.path.join('Test', file_name)
tree = ET.parse(full_path)
myroot = tree.getroot()
for test in myroot.findall('bla/blub/test/'):
print(test.attrib)
for fw in test:
print(fw.tag)
for dn in fw.findall('name'):
hostname = dn.text
如果我尝试使用 Python 3 运行代码,之后它将不会使用 for 循环:
print(fw.tag)
打印显示我在正确的位置:
{'name': 'test1'}
etc
name
任何想法,为什么代码不适用于 Python 3?
【问题讨论】:
标签: python python-3.x python-2.7 xml.etree