views:

103

answers:

1

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()
+2  A: 

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

myfile = getfile.read()
ChristopheD
also, pass the resulting string to parseString, rather than the file object
Sam Brightman