Reputation: 167
I want to save an XML file with a comment, but even if I add the comment before adding the text, the comment appears after the text in the output. My code and output is below.
def save_xml(data):
root = etree.Element('root')
student_xml = etree.ElementTree(root)
student = etree.SubElement(root,'students')
student.append(etree.Comment('\n学生信息表\n\"id\": [名字,数学,语文,英语]\n'))
student.text = str(data)
file = open('student.xml', 'w',encoding='utf-8')
file.write(etree.tounicode(student_xml.getroot()))
file.close()
<root><students>{1: ['张三', 150, 120, 100], 2: ['李四', 90, 99, 95], 3: ['王 五', 60, 66, 68]}<!--
学生信息表
"id": [名字,数学,语文,英语]
--></students></root>
And I want the output like below.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<students>
<!--
学生信息表
"id" : [名字, 数学, 语文, 英文]
-->
{
"1" : ["张三", 150, 120, 100],
"2" : ["李四", 90, 99, 95],
"3" : ["王五", 60, 66, 68]
}
</students>
</root>
Upvotes: 3
Views: 1704
Reputation: 50967
Add the text as the tail
of the comment node.
student = etree.SubElement(root, 'students')
comment = etree.Comment('\n学生信息表\n\"id\": [名字,数学,语文,英语]\n')
comment.tail = "\n" + str(data)
student.append(comment)
The tail
property contains the text immediately following an element (a comment is a special type of element node).
See also https://lxml.de/tutorial.html#elements-contain-text and http://infohost.nmt.edu/~shipman/soft/pylxml/web/etree-view.html.
If pretty-printing is important, there are a few things you can do:
Serialize using pretty_print=True
.
Use pprint.pformat()
to format the data
dictionary.
Add some extra whitespace in a few places.
Complete example:
from lxml import etree
from pprint import pformat
data = {
"1" : ["张三", 150, 120, 100],
"2" : ["李四", 90, 99, 95],
"3" : ["王五", 60, 66, 68]
}
root = etree.Element('root')
student = etree.SubElement(root, 'students')
student.text = "\n"
comment = etree.Comment('\n 学生信息表\n \"id\": [名字,数学,语文,英语]\n')
comment.tail = "\n" + pformat(data, width=35) + "\n"
student.append(comment)
etree.ElementTree(root).write("students.xml", pretty_print=True,
encoding="UTF-8", xml_declaration=True)
Contents of students.xml:
<?xml version='1.0' encoding='UTF-8'?>
<root>
<students>
<!--
学生信息表
"id": [名字,数学,语文,英语]
-->
{'1': ['张三', 150, 120, 100],
'2': ['李四', 90, 99, 95],
'3': ['王五', 60, 66, 68]}
</students>
</root>
Upvotes: 2