[英]how to insert newline in python xml.etree module
我想这样打印出xml:
<xml>
<tag>
this is line 1.
this is line 2.
</tag>
</xml>
我有一段这样的代码:
from xml.etree import ElementTree as ET
xml = ET.Element('xml')
tag = ET.SubElement(xml, 'tag')
tag.text = 'this is line 1.' + '
' + 'this is line 2.'
tree = ET.ElementTree(xml)
tree.write('test.xml')
但是它打印出来像这样:
<xml>
<tag>this is line 1.
this is line 2.</tag>
</xml>
当我使用'\\n'
而不是'
'
,输出如下:
<xml>
<tag>this is line 1. this is line 2.</tag>
</xml>
如何在“这是第1 newline
之间插入newline
。 和“这是第2行”。
使用'\\ n'换行Ie
from xml.etree import ElementTree as ET
xml = ET.Element('xml')
tag = ET.SubElement(xml, 'tag')
tag.text = 'this is line 1.' + '\n' + 'this is line 2.'
tree = ET.ElementTree(xml)
tree.write('test.xml')
会产生
<xml><tag>this is line 1.
this is line 2.</tag></xml>
这相当于
<xml>
<tag>
this is line 1.
this is line 2.
</tag>
</xml>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.