我有几个从墨迹空间生成的.svg格式的地面实况文件。为了验证用python编写的程序的精确度,我需要从.svg文件中读取'rect‘字段、它们的坐标和其他属性,如轮廓颜色和id。
我找到了这个主题:Library to parse SVG in Ruby or Python
建议在哪里使用pysvg库,但是我找不到关于psvg.parser.parse模块的文档。
有什么建议吗?
谢谢
发布于 2019-05-13 21:15:52
您可以使用python xml解析器,因为svg是一种xml。使用xpath或findall提取所需的元素,并读取元素属性以提取所需的信息:
import xml.etree.ElementTree as ET
import re
# for parsing svg as a string:
svg = ET.fromstring(svg_string)
# for parsing svg from a file:
svg = ET.parse(svg_file)
rects = svg.findall('rect')
for rect in rects:
width = rect.attrib['width']
height = rect.attrib['height']
x = rect.attrib['x']
y = rect.attrib['y']请记住,如果rect元素是组的一部分,则还必须解析它们所属的所有组的转换。这可能会变得相当棘手。
https://stackoverflow.com/questions/35968744
复制相似问题