Python3 error: initial_value must be str or None, with StringIO

response.read() returns an instance of bytes while StringIO is an in-memory stream for text only. Use BytesIO instead. From What’s new in Python 3.0 – Text Vs. Data Instead Of Unicode Vs. 8-bit The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.

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()}

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

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)