Python: lxml returns 来自带有 xpath 的 svg 的空列表
Python: lxml returns empty list from svg with xpath
我想从以下 svg 访问 tspan
-标签。为此,我读到我应该使用 lxml 和 xpath,通常 element.xpath("tspan")
应该 return 一个包含所有 tspan
标签的列表,或者?但我只得到 []
.
Python代码:
from lxml import etree
folder_input = "input"
folder_output = "output"
# generate ready to use vue components
def generate():
svgFile = open(folder_input + "/secondfloor.svg", "r")
element = etree.parse(svgFile)
print(element)
tspans = element.xpath("tspan")
print(tspans)
for tspan in tspans:
print(tspan.text)
print("END")
SVG 文件:
<svg width="687px" height="1519px" viewBox="0 0 687 1519" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Firstfloor" transform="translate(-229.000000, -29.000000)">
<g id="firstfloor" transform="translate(233.000000, 33.000000)">
<g id="rooms">
<g id="A1.06" transform="translate(428.000000, 1061.000000)">
<polyline id="Shape" stroke="#FFFFFF" stroke-width="7" fill="#FF0000" points="7.09 222.98 251.03 222.98 251.03 0.27 7.09 0.27 7.09 173.16"></polyline>
<path d="M7.09,222.98 L7.09,202.66" id="Shape" stroke="#FFFFFF" stroke-width="7"></path>
<path d="M0.16,173.91 L14.49,173.91" id="Shape" stroke="#FFFFFF" stroke-width="7"></path>
<path d="M0.24,202.66 L14.58,202.66" id="Shape" stroke="#FFFFFF" stroke-width="7"></path>
<text id="text" font-family="ArialMT, Arial" font-size="36" font-weight="normal" fill="#FFFFFF">
<tspan x="85.4580078" y="61">Line1</tspan>
<tspan x="85.4580078" y="102">Line2</tspan>
<tspan x="85.4580078" y="143">Line3</tspan>
<tspan x="85.4580078" y="184">Line4</tspan>
</text>
</g>
<g id="A1.10" transform="translate(429.000000, 761.000000)">
<polyline id="Shape" stroke="#FFFFFF" stroke-width="7" fill="#FF0000" points="14.49 200.07 0.16 200.07 6.93 200.18 7.05 225.48 250.03 225.48 250.03 226.84 250.03 213.43 250.03 0.46 7.05 0.46 7.05 171.07 0.24 171.07 14.58 171.07"></polyline>
<text id="text" font-family="ArialMT, Arial" font-size="36" font-weight="normal" fill="#FFFFFF">
<tspan x="81.4580078" y="70">Line1</tspan>
<tspan x="81.4580078" y="111">Line2</tspan>
<tspan x="81.4580078" y="152">Line3</tspan>
<tspan x="81.4580078" y="193">Line4</tspan>
</text>
</g>
</g>
</g>
</g>
</g>
</svg>
您必须为 svg
添加命名空间前缀。此外,如果您尝试从文档中的任何位置 select,您的 xpath 应该以 //
开头。作为旁注,您应该在完成解析后关闭文件。
from lxml import etree
folder_input = "input"
folder_output = "output"
# generate ready to use vue components
def generate():
svgFile = open(folder_input + "/secondfloor.svg", "r")
element = etree.parse(svgFile)
svgFile.close()
print(element)
ns = {'s': 'http://www.w3.org/2000/svg'}
tspans = element.xpath("//s:tspan", namespaces=ns)
print(tspans)
for tspan in tspans:
print(tspan.text)
print("END")
我想从以下 svg 访问 tspan
-标签。为此,我读到我应该使用 lxml 和 xpath,通常 element.xpath("tspan")
应该 return 一个包含所有 tspan
标签的列表,或者?但我只得到 []
.
Python代码:
from lxml import etree
folder_input = "input"
folder_output = "output"
# generate ready to use vue components
def generate():
svgFile = open(folder_input + "/secondfloor.svg", "r")
element = etree.parse(svgFile)
print(element)
tspans = element.xpath("tspan")
print(tspans)
for tspan in tspans:
print(tspan.text)
print("END")
SVG 文件:
<svg width="687px" height="1519px" viewBox="0 0 687 1519" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Firstfloor" transform="translate(-229.000000, -29.000000)">
<g id="firstfloor" transform="translate(233.000000, 33.000000)">
<g id="rooms">
<g id="A1.06" transform="translate(428.000000, 1061.000000)">
<polyline id="Shape" stroke="#FFFFFF" stroke-width="7" fill="#FF0000" points="7.09 222.98 251.03 222.98 251.03 0.27 7.09 0.27 7.09 173.16"></polyline>
<path d="M7.09,222.98 L7.09,202.66" id="Shape" stroke="#FFFFFF" stroke-width="7"></path>
<path d="M0.16,173.91 L14.49,173.91" id="Shape" stroke="#FFFFFF" stroke-width="7"></path>
<path d="M0.24,202.66 L14.58,202.66" id="Shape" stroke="#FFFFFF" stroke-width="7"></path>
<text id="text" font-family="ArialMT, Arial" font-size="36" font-weight="normal" fill="#FFFFFF">
<tspan x="85.4580078" y="61">Line1</tspan>
<tspan x="85.4580078" y="102">Line2</tspan>
<tspan x="85.4580078" y="143">Line3</tspan>
<tspan x="85.4580078" y="184">Line4</tspan>
</text>
</g>
<g id="A1.10" transform="translate(429.000000, 761.000000)">
<polyline id="Shape" stroke="#FFFFFF" stroke-width="7" fill="#FF0000" points="14.49 200.07 0.16 200.07 6.93 200.18 7.05 225.48 250.03 225.48 250.03 226.84 250.03 213.43 250.03 0.46 7.05 0.46 7.05 171.07 0.24 171.07 14.58 171.07"></polyline>
<text id="text" font-family="ArialMT, Arial" font-size="36" font-weight="normal" fill="#FFFFFF">
<tspan x="81.4580078" y="70">Line1</tspan>
<tspan x="81.4580078" y="111">Line2</tspan>
<tspan x="81.4580078" y="152">Line3</tspan>
<tspan x="81.4580078" y="193">Line4</tspan>
</text>
</g>
</g>
</g>
</g>
</g>
</svg>
您必须为 svg
添加命名空间前缀。此外,如果您尝试从文档中的任何位置 select,您的 xpath 应该以 //
开头。作为旁注,您应该在完成解析后关闭文件。
from lxml import etree
folder_input = "input"
folder_output = "output"
# generate ready to use vue components
def generate():
svgFile = open(folder_input + "/secondfloor.svg", "r")
element = etree.parse(svgFile)
svgFile.close()
print(element)
ns = {'s': 'http://www.w3.org/2000/svg'}
tspans = element.xpath("//s:tspan", namespaces=ns)
print(tspans)
for tspan in tspans:
print(tspan.text)
print("END")