jjzjj

toprettyxml

全部标签

python - Node.toprettyxml() 在 Python 中向 DOCTYPE 添加换行符

使用prettify时,我的DOCTYPE被分成三行。我怎样才能把它放在一条线上?“损坏”的输出:脚本:importcsvimportsysimportos.pathfromxml.etreeimportElementTreefromxml.etree.ElementTreeimportElement,SubElement,Comment,tostringfromxml.domimportminidomdefprettify(doctype,elem):"""Returnapretty-printedXMLstringfortheElement."""rough_string=doct

python - 使用 toprettyxml() 时出现换行问题

我目前在Python脚本中使用xml.dom模块的toprettyxml()函数,我在换行时遇到了一些问题。如果不使用newl参数或者如果我使用toprettyxml(newl='\n')它会显示多个换行符而不是一个。例如f=open(filename,'w')f.write(dom1.toprettyxml(encoding='UTF-8'))f.close()显示:有谁知道问题出在哪里以及我该如何使用它?仅供引用,我正在使用Python2.6.1 最佳答案 我找到了另一个很好的解决方案:f=open(filename,'w')d

python - 使用 minidom.toprettyxml 时的空行

我一直在使用minidom.toprettyxml来美化我的xml文件。当我创建XML文件并使用此方法时,一切正常,但如果我在修改xml文件后使用它(例如,我添加了一个额外的节点),然后我将它写回XML,我得到空行,每次我更新它,我得到越来越多的空行......我的代码:file.write(prettify(xmlRoot))defprettify(elem):rough_string=xml.tostring(elem,'utf-8')//xmlasElementTreereparsed=mini.parseString(rough_string)//miniasminidomre