XML 日志文件打印到漂亮
XML log file printing to pretty
我正在尝试使用 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()
如何改进此类输入的代码?做错了什么?谢谢大家的指点。
不久前有人提出问题并找到了解决方案。
我已经从 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 结构格式化输出文件。尝试了 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()
如何改进此类输入的代码?做错了什么?谢谢大家的指点。
不久前有人提出问题并找到了解决方案。
我已经从 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)
现在输出看起来像预期的那样。