在使用Python3和ElementTree生成.SVG文件時遇到了問題。在Python3中,ElementTree TypeError「write()參數必須是str,而不是字節」
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(文件)生成類型錯誤 「寫()參數必須海峽,不是字節」。我不明白這種行爲,「et」應該將ElementTree元素轉換爲字符串?它適用於python2,但不適用於python3。我做錯了什麼?
您是否檢查過文檔?查看[本頁](https://docs.python.org/3/library/xml.etree.elementtree.html)並搜索'tostring'。這有幫助嗎? –
不是真的,它應該已經在utf-8字節串中解碼,但是python3似乎有問題 –