gpt4 book ai didi

python - 用 Beautiful Soup 索引 xml/svg 文件

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:52 25 4
gpt4 key购买 nike

我有一个 svg 文件,其标题具有唯一 ID,如下所示:

<title id="t0">
<title id="t1">

这些标题有数千种。

下面是我如何从 svg 创建 BS 对象:

svg = open('counties.svg', 'r').read()
soup = BeautifulSoup(svg, 'xml')
paths = soup.findAll('path')

所以我可以拉出每条路径的样式,但我不能以相同的方式拉出标题:

for p in paths:
p['style'] = "font-size:12px..." #this works
p['title'] = "title for path..." #this doesnt work

我怀疑这是因为“标题”部分有一个唯一的 ID。

这是“路径”的示例

  <path d="M 405.190,251.879 L 405.748,257.292 L 401.350,257.81 L 401.012,254.629 L 400.885,253.474 L 401.300,253.43 L 401.430,253.322 L 401.300,252.294 L 405.190,251.879" id="01031" style="font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;
stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;
marker-start:none;stroke-linejoin:bevel;fill:#bcbddc">
<title id="t31">
Coffee, AL
</title>
</path>

如何正确索引标题以便更改其内容?

最佳答案

尝试:

for p in paths:
p['style'] = "font-size:102px" #this works
p.title.string = "title for path..."

关于python - 用 Beautiful Soup 索引 xml/svg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50666816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com