首页 > 求教: xml.etree.ElementTree的ElementTree.write(sys.stdout)的报错

求教: xml.etree.ElementTree的ElementTree.write(sys.stdout)的报错

#!/usr/bin/env python
# encoding: utf-8

"""Creating XML documents
"""

import sys
from xml.etree.ElementTree import (Element, SubElement, Comment, ElementTree, )

top = Element('top')

comment = Comment('Generated for PyMOTW')
top.append(comment)

child = SubElement(top, 'child')
child.text = 'This child contains text.'

child_with_tail = SubElement(top, 'child_with_tail')
child_with_tail.text = 'This child has regular text.'
child_with_tail.tail = 'And "tail" text.'

child_with_entity_ref = SubElement(top, 'child_with_entity_ref')
child_with_entity_ref.text = 'This & that'

empty_child = SubElement(top, 'empty_child')

ElementTree(top).write(sys.stdout)

这段代码在2.6.6中成功运行,输出结果为:

<top><!-- Generated for PyMOTW --><child>This child contains text.</child><child_with_tail>This child has regular text.</child_with_tail>And "tail" text.<child_with_entity_ref>This &amp; that</child_with_entity_ref><empty_child /></top>

但是到了3.4.3版本下,执行ElementTree(top).write(sys.stdout)时报错,
我查了标准库文档,似乎没有问题.请问如何更改才能达到原有的目标.

【热门文章】
【热门文章】