python - 属性错误 : 'ElementTree' object has no attribute 'tag' in Python

标签 python xml python-3.x

如果节点包含特定字符串(关键字),我想解析一个 XML 文件并提取我感兴趣的节点。但是要使用 find 和 finall 函数,首先我决定将我拥有的关键字列表以及 XML 文件小写。 这是代码。

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import tostring
import csv
tree=ET.parse('/Users/m/Documents/dr.xml')
**t = tostring(tree)**
t = t.lower()
tree= ET.fromstring(t).......

我在这一行遇到错误:

t = tostring(tree)

知道如何解决这个问题吗? 谢谢

最佳答案

你需要从 root node 解析它

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import tostring
tree=ET.parse('t.xml')
tree = tree.getroot()
t = tostring(tree)
t = t.lower()
tree= ET.fromstring(t)

关于python - 属性错误 : 'ElementTree' object has no attribute 'tag' in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51660316/

相关文章:

php - 从 SQL 查询创建 XML 文件

javascript - eBay 查找 API findItemsByKeywords 返回 'Input URL gave a value for header X-EBAY-SOA-OPERATION-NAME but has a conflicting mapped value' 错误

django - TemplateDoesNotExist 在/django

python - 如何在不编写完整的Dockerfile的情况下运行简单的Python脚本?

python - 有没有办法在 python 中显示原始图像?

android - Android ActionBar 中的操作项不可点击

python - 保存到 Parquet 会在 Dask.dataframe 中引发错误

python - 如何在 Python ClientForm 中设置一个不存在的字段?

python - 从 artifactory 下载文件,使用 API Key 进行身份验证

python - 如何将关键字参数作为参数传递给函数?