python-2.7 - ElementTree.tostring(): UnicodeDecodeError:
问题描述
我从这个简单的片段中得到一个例外:
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import xml.etree.cElementTree as ET
tables = ET.Element('tables')
table = ET.SubElement(tables, 'table', name='my_table')
head = dict()
tr_th = ET.SubElement(table, 'tr')
head = sorted(head.keys())
ET.SubElement(tr_th, 'th').text = 'ü'
tr_td = ET.SubElement(table, 'tr')
ET.SubElement(tr_td, 'td').text = 'ä'
ET.tostring(tables, encoding='utf8', method='xml')
例外:
user@host:~$ python ~/src/test_etree.py
Traceback (most recent call last):
File "/home/modlink_cok_d/src/test_etree.py", line 13, in <module>
ET.tostring(tables, encoding='utf8', method='xml')
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1127, in tostring
return "".join(data)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0:
ordinal not in range(128)
Python 2.7.12
我做错了什么?
解决方案
推荐阅读
- javascript - 提交后如何关闭表单模式?
- c - C 集合和指针错误 - 来自不兼容的指针类型的赋值
- selenium - 未捕获的 DOMException:无法在“CommandLineAPI”上执行“$x”:由于使用了特殊字符
- python - 不支持 Python Zipfile 压缩方法
- discord - Error when outputting an image inside an embed with canvas | Discord.js
- python - 如何解决 Python 中类方法的 NameError?
- spring-boot - SpringBoot OAuth2 error "Full authentication is required to access this resource"
- pytest - How does Pytest figure out that the monkeypatch fixture is for monkeypatching?
- c# - Discord.Net 使用 Bots 头像作为 IconURL/AvatarURL
- symfony - 如何通过在 Symfony 5 中注销将用户电子邮件放入 Logger 文件