开发者

Python: Pretty printing a xml file directly from a tar.gz package

This is the first Python script I've tried to create. I'm reading a xml file from a tar.gz package and then I want to pretty print it. However I can't seem to turn it from a file-like object to a string. I've tried to do it a few different ways including str(), tostring(), etc but nothing is working for me.

For tes开发者_高级运维ting I just tried to print the string at "print myfile[0:200]" and it always generates "<tarfile.ExFileObject object at 0x10053df10>"

import os
import sys
import tarfile
from xml.dom.minidom import parseString

tar = tarfile.open("data/ucd.all.flat.tar.gz", "r")
getfile = tar.extractfile("ucd.all.flat.xml")

myfile = str(getfile)
print myfile[0:200]

output = parseString(getfile).toprettyxml()
print output

tar.close()


Untested but you probably just need a read() call on the file-like object returned by tarfile, e.g.:

myfile = getfile.read()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜