trying to gzip uncompress a StringIO
John Machin
sjmachin at lexicon.net
Tue May 22 18:26:09 EDT 2007
On 23/05/2007 7:48 AM, Gabriel Genellina wrote:
> En Tue, 22 May 2007 14:22:25 -0300, <bob at snee.com> 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
More information about the Python-list
mailing list