Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ElementTree类型错误“;write()参数必须是str,而不是bytes;在蟒蛇3中_Python_Svg_Elementtree - Fatal编程技术网

Python ElementTree类型错误“;write()参数必须是str,而不是bytes;在蟒蛇3中

Python ElementTree类型错误“;write()参数必须是str,而不是bytes;在蟒蛇3中,python,svg,elementtree,Python,Svg,Elementtree,使用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

使用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()
从xml.etree导入ElementTree作为et
doc=et.Element('svg',width='480',height='360',version='1.1',xmlns='1http://www.w3.org/2000/svg')
#与et和doc一起做事
f=open('sample.svg','w')
f、 写入('\n')
f、 写入('\n')
f、 写入(et.tostring(doc))
f、 关闭()
函数et.tostring(doc)生成TypeError“write()参数必须是str,而不是bytes”。我不明白行为“et”应该将ElementTree元素转换成字符串吗?它在python2中工作,但在python3中不工作。我做错了什么?

试试:

f.write(et.tostring(doc).decode(encoding))
例如:

f.write(et.tostring(doc).decode("utf-8"))

事实证明,
tostring
,不管它的名称如何,确实返回了一个类型为
bytes
的对象

奇怪的事情发生了。不管怎样,这就是证据:

>>> from xml.etree.ElementTree import ElementTree, tostring
>>> import xml.etree.ElementTree as ET
>>> element = ET.fromstring("<a></a>")
>>> type(tostring(element))
<class 'bytes'>
>>从xml.etree.ElementTree导入ElementTree,tostring
>>>将xml.etree.ElementTree作为ET导入
>>>element=ET.fromstring(“”)
>>>类型(tostring(元素))
很傻,不是吗

幸运的是,您可以这样做:

>>> type(tostring(element, encoding="unicode"))
<class 'str'>
>>类型(tostring(element,encoding=“unicode”))
是的,我们都认为字节的荒谬性,那个古老的、四十多年的、过时的、叫做
ascii
的编码已经过时了


不要让我知道他们把
“unicode”
称为编码

在写入xml文件时指定字符串的编码

类似于使用
write()
进行解码(UTF-8)。
示例:
file.write(etree.tostring(doc.decode(UTF-8))
对我来说,首先创建一些模板xml(仅定义根)然后解析它是最简单的

docXml = ET.parse('template.xml')
root = docXml.getroot()
然后在我的xml中做我想做的事情,然后让他们打印出来

docXml.write("output.xml", encoding="utf-8")

输出文件应为二进制模式

f = open('sample.svg', 'wb')

你检查文件了吗?请参阅并搜索
tostring
。这有帮助吗?不是真的,它应该已经在utf-8中通过TestRing解码了,但是python3似乎有一个问题,测试出来很有趣。当我看到
type(tostring(element))
的结果时,我简直不敢相信。然后看到结果因为参数值的改变而改变。哇!那真的很奇怪。问得好。