stringio

Download and decompress gzipped file in memory?

Download and decompress gzipped file in memory? Question: I would like to download a file using urllib and decompress the file in memory before saving. This is what I have right now: response = urllib2.urlopen(baseURL + filename) compressedFile = StringIO.StringIO() compressedFile.write(response.read()) decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode=’rb’) outfile = open(outFilePath, ‘w’) outfile.write(decompressedFile.read()) This ends up writing empty …

Total answers: 4

Do I have to do StringIO.close()?

Do I have to do StringIO.close()? Question: Some code: import cStringIO def f(): buffer = cStringIO.StringIO() buffer.write(‘something’) return buffer.getvalue() The documentation says: StringIO.close(): Free the memory buffer. Attempting to do further operations with a closed StringIO object will raise a ValueError. Do I have to do buffer.close(), or it will happen automatically when buffer goes …

Total answers: 4

StringIO replacement that works with bytes instead of strings?

StringIO replacement that works with bytes instead of strings? Question: Is there any replacement for python StringIO class, one that will work with bytes instead of strings? It may not be obvious but if you used StringIO for processing binary data you are out of luck with Python 2.7 or newer. Asked By: sorin || …

Total answers: 3

When is StringIO used, as opposed to joining a list of strings?

When is StringIO used, as opposed to joining a list of strings? Question: Using StringIO as string buffer is slower than using list as buffer. When is StringIO used? from io import StringIO def meth1(string): a = [] for i in range(100): a.append(string) return ”.join(a) def meth2(string): a = StringIO() for i in range(100): a.write(string) …

Total answers: 4

how do I clear a stringio object?

how do I clear a stringio object? Question: I have a stringio object created and it has some text in it. I’d like to clear its existing values and reuse it instead of recalling it. Is there anyway of doing this? Asked By: Incognito || Source Answers: TL;DR Don’t bother clearing it, just create a …

Total answers: 3

What is the best way to write the contents of a StringIO to a file?

What is the best way to write the contents of a StringIO to a file? Question: What is the best way to write the contents of a StringIO buffer to a file ? I currently do something like: buf = StringIO() fd = open(‘file.xml’, ‘w’) # populate buf fd.write(buf.getvalue ()) But then buf.getvalue() would make …

Total answers: 2

Retrieving the output of subprocess.call()

Retrieving the output of subprocess.call() Question: How can I get the output of a process run using subprocess.call()? Passing a StringIO.StringIO object to stdout gives this error: Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py”, line 444, in call return Popen(*popenargs, **kwargs).wait() File “/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py”, line 588, in __init__ errread, errwrite) …

Total answers: 7

How do I wrap a string in a file in Python?

How do I wrap a string in a file in Python? Question: How do I create a file-like object (same duck type as File) with the contents of a string? Asked By: Daryl Spitzer || Source Answers: For Python 2.x, use the StringIO module. For example: >>> from cStringIO import StringIO >>> f = StringIO(‘foo’) …

Total answers: 4