为什么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'发布于 2021-02-07 17:36:24
使用decode()进行尝试,如
print(xmlText.decode())看看能不能用。
https://stackoverflow.com/questions/66090981
复制相似问题