stringio

python : post data within stringIO through poster?

谁说我不能喝 提交于 2019-12-23 17:12:10
问题 params = {'file': open("test.txt", "rb"), 'name': 'upload test'} datagen, headers = poster.encode.multipart_encode(params) request = urllib2.Request(upload_url, datagen, headers) result = urllib2.urlopen(request) I use poster library to POST for HTTP. It works well. I'm satisfied with that. But I want to try something. As you see above, to send file data, I have to OPEN a file. But is there any way not to make a real file to do that? We can use a STREAM, like StringIO , to deal with data like

fast way to read from StringIO until some byte is encountered

允我心安 提交于 2019-12-23 08:54:10
问题 Suppose I have some StringIO (from cStringIO ). I want to read buffer from it until some character/byte is encountered, say 'Z', so: stringio = StringIO('ABCZ123') buf = read_until(stringio, 'Z') # buf is now 'ABCZ' # strinio.tell() is now 4, pointing after 'Z' What is fastest way to do this in Python? Thank you 回答1: I very disappointed that this question get only one answer on stack overflow, because it is interesting and relevant question. Anyway, since only ovgolovin give solution and I

How can I get the response body from pycurl multi curl requests

爱⌒轻易说出口 提交于 2019-12-22 13:56:48
问题 I am unable to get anything but empty responses when performing curl multi requests. No exceptions are thrown, but the response value has no content (commented in the below snippet) Here's a simplified version of my code: from StringIO import StringIO import pycurl class CurlStream(object): curl_count = 0 curl_storage = [] def __init__(self): self.curl_multi = pycurl.CurlMulti() def add_request(self, request, post_fields=None): self.curl_count += 1 curl = self._create_curl(request, post

Django response that contains a zip file with multiple csv files

本秂侑毒 提交于 2019-12-21 22:55:28
问题 I have a algorithm that outputs a list of tuples which is ready to be written into a csv file. I'm trying to write 3 csv files (through StringIO so no writing to disk) and then zip them altogether. After that I want to attach that to the response of a django request. I'm not sure what's the most efficient way to do this. Should I use StringIO to store the 3 calls through my algo? Should I actually create the csv files first before zipping them? Can I directly use 1 zipfile call without the

Python Flask send_file StringIO blank files

混江龙づ霸主 提交于 2019-12-21 03:34:06
问题 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

Can I use cStringIO the same as StringIO?

最后都变了- 提交于 2019-12-20 17:34:47
问题 I did this: import cStringIO.StringIO as StringIO And I realize I've been using it everywhere. Is that fine? Is it treated the same as StringIO? 回答1: They are not the same. cStringIO doesn't correctly handle unicode characters. >>> StringIO.StringIO().write(u'\u0080') >>> cStringIO.StringIO().write(u'\u0080') Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128) 回答2: Nor

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

坚强是说给别人听的谎言 提交于 2019-12-20 10:37:10
问题 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.

Convert io.BytesIO to io.StringIO to parse HTML page

a 夏天 提交于 2019-12-18 14:13:12
问题 I'm trying to parse a HTML page I retrieved through pyCurl but the pyCurl WRITEFUNCTION is returning the page as BYTES and not string, so I'm unable to Parse it using BeautifulSoup. Is there any way to convert io.BytesIO to io.StringIO? Or Is there any other way to parse the HTML page? I'm using Python 3.3.2. 回答1: A naive approach: # assume bytes_io is a `BytesIO` object byte_str = bytes_io.read() # Convert to a "unicode" object text_obj = byte_str.decode('UTF-8') # Or use the encoding you

How to read image from in memory buffer (StringIO) or from url with opencv python library

▼魔方 西西 提交于 2019-12-18 11:23:17
问题 Just share a way to create opencv image object from in memory buffer or from url to improve performance. Sometimes we get image binary from url, to avoid additional file IO, we want to imread this image from in memory buffer or from url, but imread only supports read image from file system with path. 回答1: To create an OpenCV image object with in memory buffer(StringIO), we can use OpenCV API imdecode, see code below: import cv2 import numpy as np from urllib2 import urlopen from cStringIO

Should we use pandas.compat.StringIO or Python 2/3 StringIO?

回眸只為那壹抹淺笑 提交于 2019-12-17 20:40:35
问题 StringIO is the file-like string buffer object we use when reading pandas dataframe from text, e.g. "How to create a Pandas DataFrame from a string?" Which of these two imports should we use for StringIO (within pandas)? This is a long-running question that has never been resolved over four years. StringIO.StringIO (Python 2) / io.StringIO (Python 3) Advantages: more stable for futureproofing code, but forces us to version-fork, e.g. see code at bottom from EmilH. pandas.compat.StringIO