【问题标题】:AttributeError: 'ElementTree' object has no attribute 'tag' in PythonAttributeError:“ElementTree”对象在 Python 中没有属性“tag”
【发布时间】:2018-08-02 18:46:30
【问题描述】:

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

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

【问题讨论】:

    标签: python xml python-3.x


    【解决方案1】:

    你需要从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)
    

    【讨论】:

    • 谢谢@Madhan Varadhodiyil
    • 没问题。@nina_dev
    猜你喜欢
    • 2019-03-05
    • 2013-09-25
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2020-09-11
    • 2018-11-19
    相关资源
    最近更新 更多