【发布时间】:2017-07-17 18:04:22
【问题描述】:
使用 Python3 和 ElementTree 生成 .SVG 文件时遇到问题。
from xml.etree import ElementTree as et
doc = et.Element('svg', width='480', height='360', version='1.1', xmlns='http://www.w3.org/2000/svg')
#Doing things with et and doc
f = open('sample.svg', 'w')
f.write('<?xml version=\"1.0\" standalone=\"no\"?>\n')
f.write('<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n')
f.write('\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n')
f.write(et.tostring(doc))
f.close()
函数 et.tostring(doc) 生成类型错误“write() 参数必须是 str,而不是字节”。我不明白这种行为,“et”应该将 ElementTree-Element 转换为字符串吗?它适用于python2,但不适用于python3。我做错了什么?
【问题讨论】:
-
您查看文档了吗?请参阅 this page 并搜索
tostring。这有帮助吗? -
不是真的,应该已经解码成utf-8字节串了,不过python3好像有问题
标签: python svg elementtree