AttributeError:“ ElementTree”对象在Python中没有属性“ tag”

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

任何想法如何解决?谢谢


潇湘沐
浏览 176回答 1
1回答

慕仙森

您需要从根节点解析它import xml.etree.ElementTree as ETfrom xml.etree.ElementTree import tostringtree=ET.parse('t.xml')tree = tree.getroot()t = tostring(tree)t = t.lower()tree= ET.fromstring(t)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python