python - 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”的值,但我不知道如何访问这些信息。
解决方案
仅供将来参考,我根据@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')
推荐阅读
- python - 如何检查一个变量的两个条件?
- fabricjs - fabricjs 剪辑路径不按尺寸工作
- vb.net - Tapi 与团队/其他 cisco 系统一起使用
- javascript - ESLint:让它理解///
VS Code 中有什么方法可以让 ESLint 理解
/// <reference path
指令,这样它就不会no-undef
为引用的 JS 文件中定义的变量/函数抛出错误?它似乎以某种方式工作,但不完全:VS Code function descri
- php - 获取当前路由规则
- bash - 遍历给定文件夹的文件 - Bash
- jquery - 按文本而不是它的值对数据表列进行排序
- grpc - 这对 proto 文件来说是一个糟糕的结构吗
- c# - 父实体上的软删除,但子/关系 EF Core 上的硬删除
- python - 无法同时运行烧瓶和 scapy