473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

trying to gzip uncompress a StringIO

bob
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?

thanks,

Bob

###########################################

import sys, base64, gzip, StringIO

infile = sys.argv[1]
outfile = sys.argv[2]

# When unencoding base64 file, write to a string that acts like a file
decodedVersion = StringIO.StringIO()

base64.decode(open(infile, 'r'),decodedVersion)

# At this point, if we write out decodedVersion.getvalue() to a file,
# gzip -d makes a proper wmf file from it, so we know everything's OK
so far.

# following based on
# http://www.diveintopython.org/http_w...mpression.html

fileObj = gzip.GzipFile(fileobj=decodedVersion);
fileContent = fileObj.read()
print len(fileContent)

May 22 '07 #1
3 6153
En Tue, 22 May 2007 14:22:25 -0300, <bo*@snee.comescribió:
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)
--
Gabriel Genellina

May 22 '07 #2
On 23/05/2007 7:48 AM, Gabriel Genellina wrote:
En Tue, 22 May 2007 14:22:25 -0300, <bo*@snee.comescribió:
>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
May 22 '07 #3
On May 23, 2:37 am, b...@snee.com wrote:
Perfect, thanks! Now I have a working WMF file and everything.

Bob
Now you just got to get your reply button working :-D

May 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Greg Bakken | last post by:
I am writing a python program that recieves 'compressed and encoded' strings from another piece of software. I can successfully uncompress and decode the strings via import base64, zlib def...
3
by: rohisingh | last post by:
I have a tar file. The content of the file are as following. rohits@sandman 12-08-04 $ tar tvf 20041208.tar drwxr-xr-x root/root 0 2004-12-08 21:39:19 20041208/ -rw-r--r-- root/root ...
7
by: ckb2102 | last post by:
Hi there, I am new to this so I apologize in advance if I am not following the right etiquette or something... I am working on a project for school. My partner has written a short program in...
10
by: Xah Lee | last post by:
today i need to use Python to decompress gzip files. since i'm familiar with Python doc and have 10 years of computing experience with 4 years in unix admin and perl, i have quickly located the...
21
by: Paul Rubin | last post by:
I've always found the string-building idiom temp_list = for x in various_pieces_of_output(): v = go_figure_out_some_string() temp_list.append(v) final_string = ''.join(temp_list) ...
2
by: Jonathan Bowlas | last post by:
Hi listers, I've written this little script to generate some html but I cannot get it to convert to a string so I can perform a replace() on the &gt;, &lt; characters that get returned. from...
0
by: erikcw | last post by:
HI all, I'm trying to use Matplotlib to render a pie chart for a django site I'm working on. I can't figure out how to get rid of the grey background, or make the dimensions square (it's...
6
by: sebastian.noack | last post by:
Hi, is there a way to or at least a reason why I can not use tarfile to create a gzip or bunzip2 compressed archive in the memory? You might might wanna answer "use StringIO" but this isn't...
2
by: somsub | last post by:
Hi all, Here is my samle code use strict ; use warnings ; use IO::Uncompress::Unzip ; When I compiled this three lines of code in win32 I got error like below.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.