python - 从python中的xml文件中提取信息
问题描述
https://github.com/peldszus/arg-microtexts/blob/master/corpus/en/micro_b001.xml
我只想提取此标签信息:
<arggraph id="micro_b001" topic_id="waste_separation" stance="pro">
这是:“micro_b001”“waste_separation”
我想将它们保存为列表
我试过这个:
myList = []
myEdgesList=[]
#read the whole text from
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.xml'):
with open(os.path.join(root, file), encoding="UTF-8") as content:
tree = ET.parse(content)
myList.append(tree)
上面的代码是正确的,它给出了每个文件的信息
<xml.etree.ElementTree.ElementTree at 0x21c893e34c0>,
但这看起来不正确
for k in myList:
arg= [e.attrib['stance'] for e in k.findall('.//arggraph') ]
print(arg)
第二个代码没有给我所需的值
解决方案
处理此问题的一种方法:
from lxml import etree
tree = etree.parse(myfile.xml)
for graph in tree.xpath('//arggraph'):
print(graph.xpath('@id')[0])
print(graph.xpath('@topic_id')[0])
输出:
micro_b001
waste_separation
推荐阅读
- c# - 列上的 DataAnnotation [Index(IsUnique = true)] 引发错误属性“索引”在此声明类型上无效
- javascript - 在节点端调用时函数中的变量不会不断更新
- angular - 通过单击事件添加 ngClass
- javascript - Azure Functions 无法更新多个 Cosmos 文档
- android - On Touch 监听器在使用 recyclerview 时对视图有不良行为
- flutter - 颤振)如何从 Joystickview 显示“onDirectionChange”值?
- plugins - Dynamics CRM + 插件代码来计算跨记录的字段总和并在另一条记录中更新
- javascript - 如何在 JavaScript 中使用 playwright 选择下拉选项?
- sql - 替换字符串的最后一个匹配子串
- powerbi - Power BI 循环依赖