替换 XML 个标签内的信息
Replacing information within XML tags
我正在尝试使用 ElementTree 替换 SVG 文件中的信息,但是我对它很陌生,没有取得太大进展。
到目前为止,我的代码是:
import xml.etree.ElementTree as ET
tree = ET.parse('path-to-file')
root = tree.getroot()
for item in root.iter('tspan'):
print(item)
然而这并没有找到任何东西。
我要查找的 SVG 文件信息的格式为:
<text
transform="matrix(0,-1,-1,0,2286,3426)"
style="font-variant:normal;font-weight:normal;font-size:123.10199738px;font-family:Arial;-inkscape-font-specification:ArialMT;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none"
id="text79724">
<tspan
x="0 71.891571 154.0006 188.22296 256.66766"
y="0"
sodipodi:role="line"
id="tspan79722"><SI1></tspan>
</text>
我特别想改变
x="0 71.891571 154.0006 188.22296 256.66766"
到x="0"
。
我不打算使用 ElementTree 来执行此操作,但是,大多数类似的 Whosebug 问题表明这是最好的主意。
正如您在问题中所述,您未设置为使用 ElementTree - 因此这是使用 beautifulsoup
:
的解决方案
data = '''<text
transform="matrix(0,-1,-1,0,2286,3426)"
style="font-variant:normal;font-weight:normal;font-size:123.10199738px;font-family:Arial;-inkscape-font-specification:ArialMT;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none"
id="text79724"><tspan
x="0 71.891571 154.0006 188.22296 256.66766"
y="0"
sodipodi:role="line"
id="tspan79722"><SI1></tspan></text>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'html.parser')
for tspan in soup.select('tspan[x]'):
if tspan['x'] == '0 71.891571 154.0006 188.22296 256.66766':
tspan['x'] = 0
print(soup.prettify())
#if writing to a new svg file, use soup instead of soup.prettify()
打印:
<text id="text79724" style="font-variant:normal;font-weight:normal;font-size:123.10199738px;font-family:Arial;-inkscape-font-specification:ArialMT;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none" transform="matrix(0,-1,-1,0,2286,3426)">
<tspan id="tspan79722" sodipodi:role="line" x="0" y="0">
<SI1>
</tspan>
</text>
CSS select 或 tspan[x]
将 select <tspan>
标签与属性 x
。然后我们检查属性 x
是否为 '0 71.891571 154.0006 188.22296 256.66766'
。如果是,我们将其设置为 0
.
我正在尝试使用 ElementTree 替换 SVG 文件中的信息,但是我对它很陌生,没有取得太大进展。
到目前为止,我的代码是:
import xml.etree.ElementTree as ET
tree = ET.parse('path-to-file')
root = tree.getroot()
for item in root.iter('tspan'):
print(item)
然而这并没有找到任何东西。
我要查找的 SVG 文件信息的格式为:
<text
transform="matrix(0,-1,-1,0,2286,3426)"
style="font-variant:normal;font-weight:normal;font-size:123.10199738px;font-family:Arial;-inkscape-font-specification:ArialMT;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none"
id="text79724">
<tspan
x="0 71.891571 154.0006 188.22296 256.66766"
y="0"
sodipodi:role="line"
id="tspan79722"><SI1></tspan>
</text>
我特别想改变
x="0 71.891571 154.0006 188.22296 256.66766"
到x="0"
。
我不打算使用 ElementTree 来执行此操作,但是,大多数类似的 Whosebug 问题表明这是最好的主意。
正如您在问题中所述,您未设置为使用 ElementTree - 因此这是使用 beautifulsoup
:
data = '''<text
transform="matrix(0,-1,-1,0,2286,3426)"
style="font-variant:normal;font-weight:normal;font-size:123.10199738px;font-family:Arial;-inkscape-font-specification:ArialMT;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none"
id="text79724"><tspan
x="0 71.891571 154.0006 188.22296 256.66766"
y="0"
sodipodi:role="line"
id="tspan79722"><SI1></tspan></text>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'html.parser')
for tspan in soup.select('tspan[x]'):
if tspan['x'] == '0 71.891571 154.0006 188.22296 256.66766':
tspan['x'] = 0
print(soup.prettify())
#if writing to a new svg file, use soup instead of soup.prettify()
打印:
<text id="text79724" style="font-variant:normal;font-weight:normal;font-size:123.10199738px;font-family:Arial;-inkscape-font-specification:ArialMT;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none" transform="matrix(0,-1,-1,0,2286,3426)">
<tspan id="tspan79722" sodipodi:role="line" x="0" y="0">
<SI1>
</tspan>
</text>
CSS select 或 tspan[x]
将 select <tspan>
标签与属性 x
。然后我们检查属性 x
是否为 '0 71.891571 154.0006 188.22296 256.66766'
。如果是,我们将其设置为 0
.