使用 toprettyxml() 时出现换行符问题 [英] Problem with newlines when I use toprettyxml()

查看:36
本文介绍了使用 toprettyxml() 时出现换行符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 Python 脚本中使用 xml.dom 模块的 toprettyxml() 函数,但在换行时遇到了一些问题.如果不使用 newl 参数或者如果我使用 toprettyxml(newl='\n') 它会显示多个换行符而不是一个.

I'm currently using the toprettyxml() function of the xml.dom module in a Python script and I'm having some trouble with the newlines. If don't use the newl parameter or if I use toprettyxml(newl='\n') it displays several newlines instead of only one.

例如

f = open(filename, 'w')
f.write(dom1.toprettyxml(encoding='UTF-8'))
f.close()

显示:

<params>


    <param name="Level" value="#LEVEL#"/>


    <param name="Code" value="281"/>


</params>

有谁知道问题出在哪里以及我如何使用它?仅供参考,我使用的是 Python 2.6.1

Does anyone know where the problem comes from and how I can use it? FYI I'm using Python 2.6.1

推荐答案

toprettyxml() 非常糟糕.这不是 Windows 和 '\r\n' 的问题.尝试任何字符串作为 newl 参数表明添加的行太多.不仅如此,还添加了其他空白(当机器读取 xml 时可能会导致您出现问题).

toprettyxml() is quite awful. It is not a matter of Windows and '\r\n'. Trying any string as the newlparameter shows that too many lines are being added. Not only that, but other blanks (that may cause you problems when a machine reads the xml) are also added.

一些解决方法可在
http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace

这篇关于使用 toprettyxml() 时出现换行符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆