stringio

Python Flask send_file StringIO blank files

半世苍凉 提交于 2019-12-03 11:00:31
I'm using python 3.5 and flask 0.10.1 and liking it, but having a bit of trouble with send_file. I ultimately want to process a pandas dataframe (from Form data, which is unused in this example but necessary in the future) and send it to download as a csv (without a temp file). The best way to accomplish this I've seen is to us StringIO. Here is the code I'm attempting to use: @app.route('/test_download', methods = ['POST']) def test_download(): buffer = StringIO() buffer.write('Just some letters.') buffer.seek(0) return send_file(buffer, as_attachment = True,\ attachment_filename = 'a_file

Do I have to do StringIO.close()?

大城市里の小女人 提交于 2019-12-03 10:45:00
问题 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 out of scope and is garbage collected? UPDATE: I did a test: import StringIO, weakref def handler(ref): print 'Buffer died!' def f():

Python's StringIO doesn't do well with `with` statements

穿精又带淫゛_ 提交于 2019-12-03 05:49:31
I need to stub tempfile and StringIO seemed perfect. Only that all this fails in an omission: In [1]: from StringIO import StringIO In [2]: with StringIO("foo") as f: f.read() --> AttributeError: StringIO instance has no attribute '__exit__' What's the usual way to provide canned info instead of reading files with nondeterministic content? The StringIO module predates the with statement. Since StringIO has been removed in Python 3 anyways, you can just use its replacement, io.BytesIO : >>> import io >>> with io.BytesIO(b"foo") as f: f.read() b'foo' this monkeypatch works for me in python2.

Fail to get data on using read() of StringIO in python

心不动则不痛 提交于 2019-12-03 03:23:35
问题 Using Python2.7 version. Below is my sample code. import StringIO import sys buff = StringIO.StringIO() buff.write("hello") print buff.read() in the above program, read() returns me nothing where as getvalue() returns me "hello". Can anyone help me out in fixing the issue? I need read() because my following code involves reading "n" bytes. 回答1: You need to reset the buffer position to the beginning. You can do this by doing buff.seek(0) . Every time you read or write to the buffer, the

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

假如想象 提交于 2019-12-03 02:36:23
问题 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) return a.getvalue() if __name__ == '__main__': from timeit import Timer string = "This is test string" print(Timer("meth1(string)", "from __main__ import meth1, string").timeit()) print(Timer("meth2(string)", "from __main__

Pycurl and io.StringIO - pycurl.error: (23, 'Failed writing body)

Deadly 提交于 2019-12-02 23:35:25
I'm porting ebay sdk to python3 and I've stumbled upon the following issue. I'm using pycurl to send some HTTP requests. Here is how I configure it: self._curl = pycurl.Curl() self._curl.setopt(pycurl.FOLLOWLOCATION, 1) self._curl.setopt(pycurl.URL, str(request_url)) self._curl.setopt(pycurl.SSL_VERIFYPEER, 0) self._response_header = io.StringIO() self._response_body = io.StringIO() self._curl.setopt(pycurl.CONNECTTIMEOUT, self.timeout) self._curl.setopt(pycurl.TIMEOUT, self.timeout) self._curl.setopt(pycurl.HEADERFUNCTION, self._response_header.write) self._curl.setopt(pycurl.WRITEFUNCTION,

Fail to get data on using read() of StringIO in python

早过忘川 提交于 2019-12-02 16:55:32
Using Python2.7 version. Below is my sample code. import StringIO import sys buff = StringIO.StringIO() buff.write("hello") print buff.read() in the above program, read() returns me nothing where as getvalue() returns me "hello". Can anyone help me out in fixing the issue? I need read() because my following code involves reading "n" bytes. You need to reset the buffer position to the beginning. You can do this by doing buff.seek(0) . Every time you read or write to the buffer, the position is advanced by one. Say you start with an empty buffer. The buffer value is "" , the buffer pos is 0 .

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

只愿长相守 提交于 2019-12-02 16:07:50
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) return a.getvalue() if __name__ == '__main__': from timeit import Timer string = "This is test string" print(Timer("meth1(string)", "from __main__ import meth1, string").timeit()) print(Timer("meth2(string)", "from __main__ import meth2, string").timeit()) Results: 16.7872819901 18.7160351276 If you measure for speed, you should

Extract zip to memory, parse contents

非 Y 不嫁゛ 提交于 2019-12-02 10:57:58
问题 I want to read the contents of a zip file into memory rather than extracting them to disc, find a particular file in the archive, open the file and extract a line from it. Can a StringIO instance be opened and parsed? Suggestions? Thanks in advance. zfile = ZipFile('name.zip', 'r') for name in zfile.namelist(): if fnmatch.fnmatch(name, '*_readme.xml'): name = StringIO.StringIO() print name # prints StringIO instances open(name, 'r') # IO Error: No such file or directory... I found a few

Python Capture reply from powershell

有些话、适合烂在心里 提交于 2019-12-02 10:04:47
The code below works when typed manually however when I run the program.py nothing prints. My ultimate goal is to retrieve this data from user pc to create an easy way to recreate shortcuts.... My users somehow lose them lol import smtplib, os, subprocess, sys from string import ascii_uppercase from cStringIO import StringIO data = os.popen(r"dir %userprofile%\desktop\*.lnk* /s/b").read() file = open("testitem.txt", "w") file.write(data) file.close() my_data = dict(zip(ascii_uppercase,open("testitem.txt"))) old_stdout = sys.stdout sys.stdout = mystdout = StringIO() for key, value in my_data