xml 文件的根目录显示为 NONE 为什么?
root of the xml file is giving as NONE why?
from elementtree import ElementTree as ET
tree= ET.parse(r'N:\myinternwork\files xml of bus systems\testonieeebus.xml','r')
root= tree.getroot()
print(root)
现在错误出现在输出中,因为它给出了 none
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<author>Giada De Laurentiis</author>
</book>
</bookstore>
下面的代码就够了。您不需要一开始就打开文件。如果您提供正确的路径,ET.parse
会为您完成。
在您的代码中,您将库作为 ET 导入,然后通过打开文件重新分配相同的变量。所以 ET.parse()
指的是你的文件对象而不是你的 elementtree
库。
import xml.etree.ElementTree as ET
tree= ET.parse('note.xml')
root= tree.getroot()
print(root.tag) # to print root
from elementtree import ElementTree as ET
tree= ET.parse(r'N:\myinternwork\files xml of bus systems\testonieeebus.xml','r')
root= tree.getroot()
print(root)
现在错误出现在输出中,因为它给出了 none
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<author>Giada De Laurentiis</author>
</book>
</bookstore>
下面的代码就够了。您不需要一开始就打开文件。如果您提供正确的路径,ET.parse
会为您完成。
在您的代码中,您将库作为 ET 导入,然后通过打开文件重新分配相同的变量。所以 ET.parse()
指的是你的文件对象而不是你的 elementtree
库。
import xml.etree.ElementTree as ET
tree= ET.parse('note.xml')
root= tree.getroot()
print(root.tag) # to print root