sách gpt4 ăn đã đi

python - Python3 中的 ElementTree TypeError "write() argument must be str, not bytes"

In lại 作者:太空狗 更新时间:2023-10-29 19:34:33 25 4
mua khóa gpt4 giày nike

使用 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('\n')
f.write(' f.write('\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n')
f.write(et.tostring(doc))
f. đóng()

函数 et.tostring(doc) 生成类型错误“write() 参数必须是 str,而不是字节”。我不明白这种行为,“et”应该将 ElementTree-Element 转换为字符串吗?它适用于 python2,但不适用于 python3。我做错了什么?

câu trả lời hay nhất

事实证明,tostring尽管它的名字,确实确实返回了一个类型为byte.

奇怪的事情发生了。无论如何,这是证明:

>>> from xml.etree.ElementTree import ElementTree, tostring
>>> import xml.etree.ElementTree as ET
>>> element = ET.fromstring("")
>>> type(tostring(element))

很傻,不是吗?

幸运的是你可以这样做:

>>> type(tostring(element, encoding="unicode"))

是的,我们都认为字节是荒谬的,而且那种名为 ascii 的古老的、已有四十多年历史的过时编码已经死了。

不要让我开始说他们将 “unicode” 称为mã hóa!!!!!!!!!!!!

关于python - Python3 中的 ElementTree TypeError "write() argument must be str, not bytes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42480442/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress