stringio

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

断了今生、忘了曾经 提交于 2019-11-30 03:15:15
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. 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 import StringIO def create_opencv_image_from_stringio(img_stream, cv2_img_flag=0): img_stream.seek(0) img_array

Python: How to get StringIO.writelines to accept unicode string?

大兔子大兔子 提交于 2019-11-29 00:07:13
问题 I'm getting a UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 34: ordinal not in range(128) on a string stored in 'a.desc' below as it contains the '£' character. It's stored in the underlying Google App Engine datastore as a unicode string so that's fine. The cStringIO.StringIO.writelines function is trying seemingly trying to encode it in ascii format: result.writelines(['blahblah',a.desc,'blahblahblah']) How do I instruct it to treat the encoding as unicode if

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

旧城冷巷雨未停 提交于 2019-11-28 13:02:53
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 pandas.compat is a 2/3 compatibility package ("without the need for 2to3") introduced back in 0.13.0 (Jan

How can I resolve TypeError with StringIO in Python 2.7?

痴心易碎 提交于 2019-11-28 09:40:45
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 You can resolve the error by simply adding a u before your string to make the string unicode: datastring = StringIO(u"""\ Country Metric

StringIO replacement that works with bytes instead of strings?

霸气de小男生 提交于 2019-11-28 06:08:27
Is there any replacement for python StringIO class, one that will work with bytes instead of strings? It may not be obvious but if you used StringIO for processing binary data you are out of luck with Python 2.7 or newer. senderle Try io.BytesIO . As others have pointed out, you can indeed use StringIO in 2.7, but BytesIO is a good choice for forward-compatibility. In Python 2.6/2.7, the io module is intended to be used for compatibility with Python 3.X. From the docs: New in version 2.6. The io module provides the Python interfaces to stream handling. Under Python 2.x, this is proposed as an

How to loop until EOF in Python?

那年仲夏 提交于 2019-11-28 05:07:51
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 of as) an initialized loop, which seems a little un-Pythonic: len_name = data.read(4) while len_name !=

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

旧时模样 提交于 2019-11-27 19:34:06
How do I create a file-like object (same duck type as File) with the contents of a string? Daryl Spitzer 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') In Python 3.0: import io with io.StringIO() as f: f.write('abcdef') print('gh', file=f) f.seek(0

how do I clear a stringio object?

坚强是说给别人听的谎言 提交于 2019-11-27 12:17:09
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? Chris Morgan 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', 'truncate', 'write', 'writelines'] >>> help(StringIO.truncate) Help on method truncate in

Download and decompress gzipped file in memory?

China☆狼群 提交于 2019-11-27 11:34:08
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 import urllib2 import StringIO import gzip baseURL = "https://www.kernel.org/pub/linux/docs/man-pages/" #

Write to StringIO object using Pandas Excelwriter?

為{幸葍}努か 提交于 2019-11-27 09:05:51
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: from xlsxwriter.workbook import Workbook writer = Workbook(io) pd.DataFrame().to_excel(writer,"sheet name")