gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:25:31 25 4
gpt4 key购买 nike

如果节点包含特定字符串(关键字),我想解析一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com