Write to StringIO object using Pandas Excelwriter?

Pandas expects a filename path to the ExcelWriter constructors although each of the writer engines support StringIO. Perhaps that should be raised as a bug/feature request in Pandas. In the meantime here is a workaround example using the Pandas xlsxwriter engine: import pandas as pd import StringIO io = StringIO.StringIO() # Use a temp filename … Read more

Extracting a zipfile to memory?

extractall extracts to the file system, so you won’t get what you want. To extract a file in memory, use the ZipFile.read() method. If you really need the full content in memory, you could do something like: def extract_zip(input_zip): input_zip=ZipFile(input_zip) return {name: input_zip.read(name) for name in input_zip.namelist()}

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

For Python 2.x, use the StringIO module. For example: >>> from cStringIO import StringIO >>> f = StringIO(‘foo’) >>> f.read() ‘foo’ I use cStringIO (which is faster), but note that it doesn’t accept Unicode strings that cannot be encoded as plain ASCII strings. (You can switch to StringIO by changing “from cStringIO” to “from StringIO”.) … Read more

python 3.x ImportError: No module named ‘cStringIO’

From Python 3.0 changelog: The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively. From the Python 3 email documentation it can be seen that io.StringIO should be used instead: from io import StringIO from email.generator import Generator fp = StringIO() g = … Read more

Retrieving the output of subprocess.call() [duplicate]

If you have Python version >= 2.7, you can use subprocess.check_output which basically does exactly what you want (it returns standard output as string). Simple example (linux version, see note): import subprocess print subprocess.check_output([“ping”, “-c”, “1”, “8.8.8.8”]) Note that the ping command is using linux notation (-c for count). If you try this on Windows … Read more