root of the xml file is giving as NONE why?

荒凉一梦 提交于 2019-12-04 06:47:38

问题


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)

now the error is in output as it is giving none

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<author>Giada De Laurentiis</author>
</book>
</bookstore>

回答1:


The following code is enough. You don't need to open the file at beginning. ET.parse will do it for you if you provide the correct path.

In your code you import the library as ET and then reassign the same variable by opening the file. So ET.parse() referes to your file object not to your elementtree library.

import xml.etree.ElementTree as ET

tree= ET.parse('note.xml')
root= tree.getroot()
print(root.tag) # to print root


来源:https://stackoverflow.com/questions/44215454/root-of-the-xml-file-is-giving-as-none-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!