我从这个简单的代码段中得到了一个例外:
# -*- 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
我做错了什么?