我有XML文件,它代表目录中文件的结构,看起来与此类似
<dir name="dirName1">
<file name="fileName1"/>
<file name="fileName2"/>
...
<file name="fileNameN"/>
</dir>
我希望找到例如文件fileName1,fileName3,fileName15,有没有比
更好的方法来做到这一点for file in filenames:
node = root.find(".//*[@name='" + file + "']")
#do something with this file
答案 0 :(得分:0)
for e in tree.findall('./fileName1'):#dirName1= root so you put ./fileName1 to have the childs
print(e.attrib.get('name'))
希望有所帮助