Questions tagged [stringio]

Use for questions concerning the Python 2 module StringIO or the class StringIO it contains. In Python 3, this refers to the class StringIO of the io module.

Python class StringIO allows to read and write a string buffer (also known as memory files).

355 questions
32
votes
1 answer

Confusing about StringIO, cStringIO and ByteIO

I have googled and also search on SO for the difference between these buffer modules. However, I still don't understand very well and I think some of the posts I read are out of date. In Python 2.7.11, I downloaded a binary file of a specific format…
wsdzbm
  • 3,096
  • 3
  • 25
  • 28
30
votes
2 answers

How can I resolve TypeError with StringIO in Python 2.7?

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 …
Amit
  • 19,780
  • 6
  • 46
  • 54
28
votes
2 answers

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

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…
evanchin
  • 2,028
  • 1
  • 22
  • 25
25
votes
2 answers

python3 print to string

Using Python 3, I have a console application that I am porting to a GUI. The code has a bunch of print statements, something like this: print(f1(), f2(), f3(), sep=getsep(), end=getend()) I would like to convert these calls into something…
Brad
  • 3,190
  • 1
  • 22
  • 36
25
votes
4 answers

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

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…
citronic
  • 9,868
  • 14
  • 51
  • 74
23
votes
4 answers

Write to StringIO object using Pandas Excelwriter?

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 =…
A User
  • 812
  • 2
  • 7
  • 21
22
votes
1 answer

What does the .rewind method do on a Tempfile in ruby?

I've looked through these docs and Google, and can't seem to find the purpose of .rewind, and how it differs from .close, in the context of working with a Tempfile. Also, why does .read return an empty string before rewinding? Here is an…
Jon E Kilborn
  • 223
  • 1
  • 2
  • 5
22
votes
3 answers

Python, write in memory zip to file

How do I write an in memory zipfile to a file? # Create in memory zip and add files zf = zipfile.ZipFile(StringIO.StringIO(), mode='w',compression=zipfile.ZIP_DEFLATED) zf.writestr('file1.txt', "hi") zf.writestr('file2.txt', "hi") # Need to write…
user984003
  • 28,050
  • 64
  • 189
  • 285
20
votes
1 answer

Python logging to StringIO handler

I have a python test in which I want to test if the logging works properly. For example I have a function that creates a user and at the end the logging writes to log file the response. logger =…
bogtan
  • 825
  • 2
  • 13
  • 23
20
votes
1 answer

Is it possible to rewind a python StringIO in-memory file?

Let's say I have a StringIO file-like object I just created from a string. I pass it to a function which expects files. This functions reads the entire file through the end. I want to now pass it to another function which expects a file-like object.…
Krystian Cybulski
  • 10,789
  • 12
  • 67
  • 98
19
votes
2 answers

Python - generate csv file in memory and then encode its data into base64?

I need to generate csv file like data in memory and then encode it to base64, so I could save it. So basically I don't want to create file in hard disk for that. Now I solve this by creating csv file then encoding its data, saving it and then just…
Andrius
  • 19,658
  • 37
  • 143
  • 243
19
votes
2 answers

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

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…
mike3996
  • 17,047
  • 9
  • 64
  • 80
17
votes
2 answers

Can I use cStringIO the same as StringIO?

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?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
16
votes
1 answer

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

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) …
Nikolay Derkach
  • 1,734
  • 2
  • 22
  • 34
14
votes
2 answers

io.StringIO encoding in python3

I can't seem to find what's the default encoding for io.StringIO in Python3. Is it the locale as with stdio? How can I change it? With stdio, seems that just reopening with correct encoding works, but there's no such thing as reopening a StringIO.
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
1
2
3
23 24