我正在尝试以 gzip
格式编写 xml 的特定子树。以下 Python
代码将常规 xml 写入文件,但我不确定如何为其添加压缩
import os, time, gzip
import xml.etree.cElementTree as ET
from xml.etree.cElementTree import ElementTree
. . .
ElementTree(child).write('outputFile.xml')
我想编写一个可以用常见应用程序打开的文件outputFile.xml.gz
。请记住,我是 Python 新手...
最佳答案
ElementTree.write() 可以传递一个打开用于写入的文件对象。
import os, time, gzip
import xml.etree.cElementTree as ET
from xml.etree.cElementTree import ElementTree
. . .
f = gzip.open('/home/joe/file.txt.gz', 'wb')
ElementTree(child).write(f)
f.close()
来自http://docs.python.org/2/library/gzip.html和 http://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree .
关于python - 编写 gzip 格式压缩的 ElementTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13202516/