XML日志文件打印到漂亮[重复]
Posted
技术标签:
【中文标题】XML日志文件打印到漂亮[重复]【英文标题】:XML log file printing to pretty [duplicate] 【发布时间】:2021-10-10 11:54:33 【问题描述】:我正在尝试使用 XML 结构格式化输出文件。尝试了 xml 和 lxml 但现在没有运气。 当前输出文件如下所示:
<root><time>2021-07-28 10:27:49,869 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_request id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:49,881 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_response id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:51,834 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_request id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:51,854 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_response id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:52,182 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_request id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:52,201 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_response id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest></root>
在此处使用此代码:
import lxml.etree as etree
xml_input = etree.parse(r'output\output.txt')
print(etree.tostring(xml_input, pretty_print=True),file = open(r'output\output.txt','a'))
file.close()
如何改进此类输入的代码?做错了什么?谢谢大家的建议。
【问题讨论】:
嘿,@mzjn,是的。我现在检查了:) 【参考方案1】:不久前提出问题并找到了解决方案。
我从How to pretty print XML from the command line?获取代码
使用 xml.dom.minidom 的地方。
import xml.dom.minidom
with open(r'output\output.txt', 'r+') as file:
s = file.read()
pretty = xml.dom.minidom.parseString(s).toprettyxml()
with open(r'output\output.txt', 'w') as writer:
writer.write(pretty)
现在输出看起来像预期的那样。
【讨论】:
以上是关于XML日志文件打印到漂亮[重复]的主要内容,如果未能解决你的问题,请参考以下文章