stringio

Extracting a zipfile to memory?

梦想与她 提交于 2019-11-27 07:34:05
How do I extract a zip to memory? My attempt (returning None on .getvalue() ): from zipfile import ZipFile from StringIO import StringIO def extract_zip(input_zip): return StringIO(ZipFile(input_zip).extractall()) mata 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()} Deviacium Frequently working with in-memory

Download and decompress gzipped file in memory?

流过昼夜 提交于 2019-11-27 04:02:57
问题 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 files. How can I achieve what I'm after? Updated Answer: #! /usr/bin/env python2

How can I resolve TypeError with StringIO in Python 2.7?

我只是一个虾纸丫 提交于 2019-11-27 03:13:52
问题 Trying to read following string as file using StringIO but getting the error below. How can I resolve it? >> from io import StringIO >>> >>> datastring = StringIO("""\ ... Country Metric 2011 2012 2013 2014 ... USA GDP 7 4 0 2 ... USA Pop. 2 3 0 3 ... GB GDP 8 7 0 7 ... GB Pop. 2 6 0 0 ... FR GDP 5 0 0 1 ... FR Pop. 1 1 0 5 ... """) Traceback (most recent call last): File "<stdin>", line 9, in <module> TypeError: initial_value must be unicode or None, not str 回答1: You can resolve the error by

How to loop until EOF in Python?

故事扮演 提交于 2019-11-27 00:48:38
问题 I need to loop until I hit the end of a file-like object, but I'm not finding an "obvious way to do it", which makes me suspect I'm overlooking something, well, obvious. :-) I have a stream (in this case, it's a StringIO object, but I'm curious about the general case as well) which stores an unknown number of records in "<length><data>" format, e.g.: data = StringIO("\x07\x00\x00\x00foobar\x00\x04\x00\x00\x00baz\x00") Now, the only clear way I can imagine to read this is using (what I think

how do I clear a stringio object?

你。 提交于 2019-11-26 15:57:38
问题 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? 回答1: TL;DR Don't bother clearing it, just create a new one—it’s faster. The method Python 2 Here's how I would find such things out: >>> from StringIO import StringIO >>> dir(StringIO) ['__doc__', '__init__', '__iter__', '__module__', 'close', 'flush', 'getvalue', 'isatty', 'next', 'read', 'readline', 'readlines', 'seek', 'tell

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

六眼飞鱼酱① 提交于 2019-11-26 15:57:14
问题 How do I create a file-like object (same duck type as File) with the contents of a string? 回答1: 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".) For Python 3.x, use the io module. f = io.StringIO('foo') 回答2

Write to StringIO object using Pandas Excelwriter?

十年热恋 提交于 2019-11-26 12:47:00
问题 I can pass a StringIO object to pd.to_csv() just fine: io = StringIO.StringIO() pd.DataFrame().to_csv(io) But when using the excel writer, I am having a lot more trouble. io = StringIO.StringIO() writer = pd.ExcelWriter(io) pd.DataFrame().to_excel(writer,\"sheet name\") writer.save() Returns an AttributeError: StringIO instance has no attribute \'rfind\' I\'m trying to create an ExcelWriter object without calling pd.ExcelWriter() but am having some trouble. This is what I\'ve tried so far:

python 3.x ImportError: No module named &#39;cStringIO&#39;

﹥>﹥吖頭↗ 提交于 2019-11-26 07:40:40
问题 How do I solve an ImportError: No module named \'cStringIO\' under Python 3.x? 回答1: 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 = Generator(fp, mangle_from_=True, maxheaderlen=60) g.flatten(msg) text

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

六月ゝ 毕业季﹏ 提交于 2019-11-25 22:37:52
问题 This question already has an answer here: Store output of subprocess.Popen call in a string 14 answers 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