Python: Pretty printing a xml file directly from a tar.gz package
Posted
by EddyR
on Stack Overflow
See other posts from Stack Overflow
or by EddyR
Published on 2010-03-23T09:31:55Z
Indexed on
2010/03/23
9:33 UTC
Read the original article
Hit count: 466
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 testing 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()
© Stack Overflow or respective owner