首页 > 解决方案 > python:从svg文件解析某些值

问题描述

我有一个如下所示的 svg 文件(示例)

<svg 
<g class="displacy-arrow">
<path class="displacy-arc" id="arrow-ec55d4518d3c43e391ffce0b97c713ab-0-2" stroke-width="2px" d="M420,89.5 C420,2.0 575.0,2.0 575.0,89.5" fill="none" stroke="currentColor"/>
<text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">
    <textPath xlink:href="#arrow-ec55d4518d3c43e391ffce0b97c713ab-0-2" class="displacy-label" startOffset="50%" side="left" fill="currentColor" text-anchor="middle">pd</textPath>
</text>
<path class="displacy-arrowhead" d="M575.0,91.5 L583.0,79.5 567.0,79.5" fill="currentColor"/>
</g>
</svg>

我尝试使用以下代码访问“textpath”节点内的内容:

import xml.dom.minidom
doc = xml.dom.minidom.parse('my_file.svg')
name = doc.getElementsByTagName('textPath')
for t in name:
    print([x.nodeValue for x in t.childNodes])

但是,我想获取“textpath”中包含的其他信息,例如“side”或“fill”的值,但我不知道如何访问这些信息。

标签: pythonxmlsvgnodes

解决方案


仅供将来参考,我根据@Aswath 在评论中发送的链接编写了一个函数

from bs4 import BeautifulSoup

def extract_data_from_report3(filename):
soup = BeautifulSoup(open(filename), "html.parser")
for element in soup.find_all('textpath'):
    print(element.get('side'))

extract_data_from_report3('my_file.svg')


推荐阅读