简体   繁体   English

python: xml.etree.elementtree.ElemenTtree.write() 声明标签

[英]python: xml.etree.elementtree.ElemenTtree.write() declaration tag

I've created an XML document using xml.etree.elementtree.Element, and wanted to print it using the ElementTree.write() function but the declaration tag that comes out is我已经使用 xml.etree.elementtree.Element 创建了一个 XML 文档,并想使用 ElementTree.write() 函数打印它,但出现的声明标记是

<?xml version='1.0' encoding='UTF-8'?>

While I need to be in double quotes.虽然我需要用双引号。 is there a way to change that?有没有办法改变它?

I had the same problem, looked in the code of the ElementTree.py and saw the following.我有同样的问题,查看ElementTree.py的代码并看到以下内容。

For the root tag (single quotes):对于根标签(单引号):

        if method == "xml":
            write("<?xml version='1.0' encoding='%s'?>\n" % encoding)

And for the attributes (double quotes):对于属性(双引号):

write(" %s=\"%s\"" % (qnames[k], v))

It's hardcoded that way...它是这样硬编码的......

I changed it (locally) to:我将它(本地)更改为:

"<?xml version=\"1.0\" encoding=\"%s\"?>\n"

So every attribute is double quoted now.所以现在每个属性都是双引号的。

Eventually I used the tostring function and appended the XML to the correct tag and then the python file.write function.最终我使用了 tostring 函数并将 XML 附加到正确的标签,然后是 python file.write 函数。 It's ugly (and im lying about the actual encoding of the file) but it works.这很丑陋(我对文件的实际编码撒谎)但它有效。

I did the same as bg1987.我和 bg1987 一样。 Here is the function I wrote in case is useful to somebody这是我写的函数以防万一对某人有用

def wrTmp(treeObject, filepath):
    xml_str = ('<?xml version="1.0" encoding="UTF-8"?>' + '\n' + xml.etree.ElementTree.tostring(treeObject.getroot(), method='xml'))
    with open(filepath, 'wb') as xml_file:
         xml_file.write(xml_str)

I had to do pretty much the same thing, except the other way around, due to hacks in various $workplace tools that demand single quotes where python's ElementTree.write puts in double quotes.我不得不做几乎相同的事情,除了相反的方式,因为各种 $workplace 工具中的黑客需要单引号,而 python 的ElementTree.write放在双引号中。 (A bit of code looks for the literal string status='ok' and does not recognize status="ok" . Yes, that code is broken—in several ways, actually—but I just have to work around it.) (一些代码查找文字字符串status='ok'并且无法识别status="ok" 。是的,该代码被破坏了——实际上在几个方面——但我只需要解决它。)

Fortunately "user data" single or double quotes are encoded as &apos;幸运的是,“用户数据”单引号或双引号被编码为&apos; and &quot;&quot; (respectively). (分别)。 In my case I was already using tostring rather than write (for other reasons), so I have:就我而言,我已经在使用tostring而不是write (出于其他原因),所以我有:

import xml.etree.ElementTree as ET
# [... mass snippage]
        text = ET.tostring(reply).replace('"', "'")
        # [... snippaage]
        self.wfile.write(text)

(Obviously you'll want replace("'", '"') instead.) (显然你会想要replace("'", '"')代替。)

Another way would be (if you also want to pretty print your xml) to use the minidom.toprettyxml(encoding="UTF-8") which puts the xml and encoding declaration in double quotes:另一种方法是(如果您还想漂亮地打印您的 xml)使用minidom.toprettyxml(encoding="UTF-8")将 xml 和编码声明放在双引号中:

from xml.dom import minidom
xmlDom = minidom.parse("my_file.xml")

prettyXML = xmlDom.toprettyxml(encoding="UTF-8")

myfile = open(fileName, mode="wb")
myfile.write(prettyXML)
myfile.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM