Re: trying to gzip uncompress a StringIO



On 23/05/2007 7:48 AM, Gabriel Genellina wrote:
En Tue, 22 May 2007 14:22:25 -0300, <bob@xxxxxxxx> escribió:

I'm using the code below to read the zipped, base64 encoded WMF file
saved in an XML file with "Save as XML" from MS Word. As the "At this
point" comment shows, I know that the base64 decoding is going fine,
but unzipping from the decodedVersion StringIO object isn't getting me
anything, because the len(fileContent) call is returning 0. Any
suggestions?

Perhaps GzipFile reads from the current position till end (and sees nothing).
Try rewinding the file:

decodedVersion = StringIO.StringIO()
base64.decode(open(infile, 'r'),decodedVersion)
decodedVersion.seek(0)
fileObj = gzip.GzipFile(fileobj=decodedVersion);
fileContent = fileObj.read()
print len(fileContent)



>>> import StringIO
>>> s = StringIO.StringIO()
>>> s.write('blahblah\n')
>>> s.read()
''
>>> s.seek(0)
>>> s.read()
'blahblah\n'
>>>

The error would have been screamingly obvious on an open-reel mag. tape drive :-)

I suggest that "perhaps" and "try" are a little too tentative :-) GzipFile (or anything else that reads a file) is entitled to assume that the file(-like) object it is given has had its read head positioned by the caller at wherever the caller wants it to read from. Callers are entitled to assume that the reader will not arbitrarily rewind (or skip backwards/forwards to tape mark!) before reading.

Cheers,
John
.



Relevant Pages

  • Re: Isotope decay chains
    ... but what about if that decay chain existed? ... i sayed that Pt cannot become Gold (the real gold only may be ... it is in thise cases of which there is a single alpha particle ... (acording to feuerbachers undestandings ...
    (sci.physics)
  • Re: Damn you, FEDEX! or Nikon D40 lost in Springfield, MO blackhole.
    ... the 2 mp Mavica he had been using with a Nikon D40. ... After shopping around, he got me to order one for him. ... The shipper had it insured, but from what I have read it could take weeks to sort this crap out. ... You may get your insurance from FedEx and a couple weeks later they find it and deliver it. ...
    (alt.photography)
  • Re: python 3 constant
    ... is require Python to change to accommodate your need. ... Can you see the slight difference? ... Best regards, ...
    (comp.lang.python)
  • Re: The Sci-Fi Rejection Letter That Time Forgot
    ... nations have stockpiled arsenals of these incredible bombs and the time the story is set. ...
    (rec.arts.sf.written)
  • Re: Wachstumssaison Flieder
    ... Handbreit über dem Boden. ... "Fliederkrimi" am Samstag halten heute meine Nerven nicht aus. ... Dorothee ...
    (de.rec.garten)