【发布时间】:2023-04-09 20:04:02
【问题描述】:
我正在使用第 3 方 python 库来创建 .svg(专门用于进化树),它具有用于树对象的 render 函数。我想要的是我可以编辑的字符串形式的svg。目前我保存svg并读取文件如下:
tree.render('location/filename.svg', other_args...)
f = open('location/filename.svg', "r")
svg_string = f.read()
f.close()
这可行,但是否可以使用临时文件代替?到目前为止,我有:
t = tempfile.NamedTemporaryFile()
tmpdir = tempfile.mkdtemp()
t.name = os.path.join(tmpdir, 'tmp.svg')
tree.render(t.name, other_args...)
svg_string = t.read()
t.close()
谁能解释为什么这不起作用和/或我如何在不创建文件的情况下做到这一点(我只需要稍后删除)。 svg_string 我继续编辑以在 django 应用程序中使用。
编辑:重要的是,渲染功能也可以用于创建其他文件类型,例如.png - 所以需要指定 .svg 扩展名。
【问题讨论】:
标签: python django svg temporary-files