[英]Problem on using lxml with tostring and pretty_print
I have read some of the answers for related questions, but none of them is directly related with lxml tostring and pretty_print.我已经阅读了一些相关问题的答案,但没有一个与 lxml tostring 和 pretty_print 直接相关。
I am using lxml and trying to create a xml file on Python 3.6.我正在使用 lxml 并尝试在 Python 3.6 上创建 xml 文件。
The problem I found is that elements are not wrapped and ordered by parent element and believe it is related with the "pretty_print" option.我发现的问题是元素没有被父元素包装和排序,并且认为它与“pretty_print”选项有关。
What I need to achieve is:我需要实现的是:
<root>
<element1></element1>
<element2></element2>
<child1></child1>
<child2></child2>
</root>
The result I get is:我得到的结果是:
<root><element1></element1><element2></element2><child1></child1><child2></child2></root>
Part of the code I am using:我正在使用的部分代码:
from lxml import etree as et
CompanyID = "Company Identification"
TaxRegistrationNumber = "Company Reg. Number"
TaxAccountingBasis = "File Tipe"
CompanyName = "Company Name"
BusinessName = "Business Name"
root = et.Element("root")
header = et.SubElement(root, 'Header')
header.tail = '\n'
data = (
('CompanyID', str(CompanyID)),
('TaxRegistrationNumber', str(TaxRegistrationNumber)),
('TaxAccountingBasis', str(TaxAccountingBasis)),
('CompanyName', str(CompanyName)),
('BusinessName', str(BusinessName)),
)
for tag, value in data:
if value is None :
continue
et.SubElement(header, tag).text=value
xml_txt = et.tostring(root, pretty_print=True, encoding="UTF-8")
print(xml_txt)
If I print the elements with no data into it, it works fine and the "pretty_print" works fine.如果我打印没有数据的元素,它工作正常,“pretty_print”工作正常。
If I add data to each of the elements (using the above variables), the "pretty_print" does not work and the structure gets messed up.如果我向每个元素添加数据(使用上述变量),“pretty_print”不起作用并且结构会变得混乱。
What could be wrong?有什么问题?
I found it.我找到了。
I have removed the "header.tail = '\n'" from the code and it's working now.我已经从代码中删除了“header.tail = '\n'”,它现在可以工作了。
root = et.Element("root")
header = et.SubElement(root, 'Header')
#header.tail = '\n'
Thank you all谢谢你们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.