如何将xml文件保存到磁盘?

时间:2022-01-27 01:03:29

I did something similar to this, but couldn't find a way to write the result to an xml file.

我做了类似的事情,但找不到将结果写入xml文件的方法。

3 个解决方案

#1


11  

The code on the web page you linked to uses doc.toprettyxml to create a string from the XML DOM, so you can just write that string to a file:

您链接到的网页上的代码使用doc.toprettyxml从XML DOM创建字符串,因此您只需将该字符串写入文件:

f = open("output.xml", "w")
try:
    f.write(doc.toprettyxml(indent="  "))
finally:
    f.close()

In Python 2.6 (or 2.7 I suppose, whenever it comes out), you can use the "with" statement:

在Python 2.6(或者我认为2.7,无论什么时候出现),你可以使用“with”语句:

with open("output.xml", "w") as f:
    f.write(doc.toprettyxml(indent="  "))

This also works in Python 2.5 if you put

如果放入,这也适用于Python 2.5

from __future__ import with_statement

at the beginning of the file.

在文件的开头。

#2


9  

coonj is kind of right, but xml.dom.ext.PrettyPrint is part of the increasingly neglected PyXML extension package. If you want to stay within the supplied-as-standard minidom, you'd say:

coonj是正确的,但xml.dom.ext.PrettyPrint是越来越被忽视的PyXML扩展包的一部分。如果你想留在标准的迷你服务范围内,你会说:

f= open('yourfile.xml', 'wb')
doc.writexml(f, encoding= 'utf-8')
f.close()

(Or using the ‘with’ statement as mentioned by David to make it slightly shorter. Use mode 'wb' to avoid unwanted CRLF newlines on Windows interfering with encodings like UTF-16. Because XML has its own mechanisms for handling newline interpretation, it should be treated as a binary file rather than text.)

(或者使用David提到的'with'语句使其略短。使用模式'wb'来避免Windows上不需要的CRLF换行符干扰UTF-16等编码。因为XML有自己的处理换行符解释的机制,所以应该被视为二进制文件而不是文本。)

If you don't include the ‘encoding’ argument (to either writexml or toprettyxml), it'll try to write a Unicode string direct to the file, so if there are any non-ASCII characters in it, you'll get a UnicodeEncodeError. Don't try to .encode() the results of toprettyxml yourself; for non-UTF-8 encodings this can generate non-well-formed XML.

如果你不包含'encoding'参数(对writexml或toprettyxml),它会尝试直接写一个Unicode字符串到文件,所以如果有任何非ASCII字符,你会得到一个UnicodeEncodeError。不要尝试.encode()你自己的toprettyxml的结果;对于非UTF-8编码,这可以生成非格式良好的XML。

There's no ‘writeprettyxml()’ function, but it's trivially simple to do it yourself:

没有'writeprettyxml()'函数,但这样做很简单:

with open('output.xml', 'wb') as f:
    doc.writexml(f, encoding= 'utf-8', indent= '    ', newl= '\n')

#3


1  

f = open('yourfile.xml', 'w')
xml.dom.ext.PrettyPrint(doc, f)
f.close()

#1


11  

The code on the web page you linked to uses doc.toprettyxml to create a string from the XML DOM, so you can just write that string to a file:

您链接到的网页上的代码使用doc.toprettyxml从XML DOM创建字符串,因此您只需将该字符串写入文件:

f = open("output.xml", "w")
try:
    f.write(doc.toprettyxml(indent="  "))
finally:
    f.close()

In Python 2.6 (or 2.7 I suppose, whenever it comes out), you can use the "with" statement:

在Python 2.6(或者我认为2.7,无论什么时候出现),你可以使用“with”语句:

with open("output.xml", "w") as f:
    f.write(doc.toprettyxml(indent="  "))

This also works in Python 2.5 if you put

如果放入,这也适用于Python 2.5

from __future__ import with_statement

at the beginning of the file.

在文件的开头。

#2


9  

coonj is kind of right, but xml.dom.ext.PrettyPrint is part of the increasingly neglected PyXML extension package. If you want to stay within the supplied-as-standard minidom, you'd say:

coonj是正确的,但xml.dom.ext.PrettyPrint是越来越被忽视的PyXML扩展包的一部分。如果你想留在标准的迷你服务范围内,你会说:

f= open('yourfile.xml', 'wb')
doc.writexml(f, encoding= 'utf-8')
f.close()

(Or using the ‘with’ statement as mentioned by David to make it slightly shorter. Use mode 'wb' to avoid unwanted CRLF newlines on Windows interfering with encodings like UTF-16. Because XML has its own mechanisms for handling newline interpretation, it should be treated as a binary file rather than text.)

(或者使用David提到的'with'语句使其略短。使用模式'wb'来避免Windows上不需要的CRLF换行符干扰UTF-16等编码。因为XML有自己的处理换行符解释的机制,所以应该被视为二进制文件而不是文本。)

If you don't include the ‘encoding’ argument (to either writexml or toprettyxml), it'll try to write a Unicode string direct to the file, so if there are any non-ASCII characters in it, you'll get a UnicodeEncodeError. Don't try to .encode() the results of toprettyxml yourself; for non-UTF-8 encodings this can generate non-well-formed XML.

如果你不包含'encoding'参数(对writexml或toprettyxml),它会尝试直接写一个Unicode字符串到文件,所以如果有任何非ASCII字符,你会得到一个UnicodeEncodeError。不要尝试.encode()你自己的toprettyxml的结果;对于非UTF-8编码,这可以生成非格式良好的XML。

There's no ‘writeprettyxml()’ function, but it's trivially simple to do it yourself:

没有'writeprettyxml()'函数,但这样做很简单:

with open('output.xml', 'wb') as f:
    doc.writexml(f, encoding= 'utf-8', indent= '    ', newl= '\n')

#3


1  

f = open('yourfile.xml', 'w')
xml.dom.ext.PrettyPrint(doc, f)
f.close()