【发布时间】:2019-01-23 20:23:28
【问题描述】:
使用 python 从 xml 文件中读取值的最佳(re:最简单)方法是什么?我是新手,尝试过使用 minidom,但不确定如何格式化脚本。
/tmp/text.xml 中的 XML:
<computer>
<location>
<username>FirsLast</username>
</location>
</computer>
我想解析用户名并将其用作变量。
这是我尝试过的:
#!/usr/bin/python
import xml.dom.minidom as minidom
doc = minidom.parse('/tmp/text.xml')
location = doc.getElementsByTagName('location')[0]
username = location.getAttribute('username')
print(username)
我根本没有得到任何结果。我希望看到FirsLast。
【问题讨论】: