xml 文件的根目录为 NONE 为什么?
- 作者: 亖呉?盀
- 来源: 51数据库
- 2022-11-01
问题描述
from elementtree import ElementTree as ET tree= ET.parse(r'N:myinternworkfiles xml of bus systems estonieeebus.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>
推荐答案
下面的代码就足够了.您不需要在开始时打开文件.如果您提供正确的路径,ET.parse 将为您完成.
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.
在您的代码中,您将库导入为 ET,然后通过打开文件重新分配相同的变量.所以 ET.parse() 指的是你的文件对象而不是你的 elementtree 库.
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
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
