如何解决Etree 无法漂亮地打印
为什么 Etree
无法漂亮打印?
我在 repl.it
中运行了以下程序:
library.xml
<?xml version="1.0" encoding="utf-8"?>
<library attrib1="att11" attrib2="att22">
library-text
<book isbn="1111111111">
<title lang="en">T1 T1 T1 T1 T1</title>
<date>2001</date>
<author>A1 A1 A1 A1 A1</author>
<price>10.00</price>
</book>
<book isbn="2222222222">
<title lang="en">T2 T2 T2 T2 T2</title>
<date>2002</date>
<author>A2 A2 A2 A2 A2</author>
<price>20.00</price>
</book>
<book isbn="3333333333">
<title lang="en">T3 T3 T3 T3</title>
<date>2003</date>
<author>A3 A3 A3 A3 A3y</author>
<price>30.00</price>
</book>
</library>
main.py
import lxml.etree as etree
dom = etree.parse("library.xml")
xmlText = etree.tostring(dom,pretty_print=True)
print(xmlText)
输出
b'<library attrib1="att11" attrib2="att22">\n\tlibrary-text\n\t<book isbn="1111111111">\n\t\t<title lang="en">T1 T1 T1 T1 T1</title>\n\t\t<date>2001</date>\n\t\t<author>A1 A1 A1 A1 A1</author>\t\t\n\t\t<price>10.00</price>\n\t</book>\n\t<book isbn="2222222222">\n\t\t<title lang="en">T2 T2 T2 T2 T2</title>\n\t\t<date>2002</date>\n\t\t<author>A2 A2 A2 A2 A2</author>\t\t\n\t\t<price>20.00</price>\n\t</book>\n\t<book isbn="3333333333">\n\t\t<title lang="en">T3 T3 T3 T3</title>\n\t\t<date>2003</date>\n\t\t<author>A3 A3 A3 A3 A3y</author>\t\t\n\t\t<price>30.00</price>\n\t</book>\n</library>\n'
解决方法
用 decode()
试试
print(xmlText.decode())
看看它是否有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。