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
331
votes
7 answers

Retrieving the output of subprocess.call()

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 "", line 1, in File…
Jeffrey Aylesworth
  • 8,242
  • 9
  • 40
  • 57
130
votes
4 answers

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

How do I create a file-like object (same duck type as File) with the contents of a string?
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
110
votes
3 answers

python 3.x ImportError: No module named 'cStringIO'

How do I solve an ImportError: No module named 'cStringIO' under Python 3.x?
jvi
  • 1,111
  • 2
  • 7
  • 5
87
votes
3 answers

Extracting a zipfile to memory?

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())
user1438003
  • 6,603
  • 8
  • 30
  • 36
83
votes
4 answers

Python3 error: initial_value must be str or None, with StringIO

While porting code from python2 to 3, I get this error when reading from a URL TypeError: initial_value must be str or None, not bytes. import urllib import json import gzip from urllib.parse import urlencode from urllib.request import…
AMisra
  • 1,869
  • 2
  • 25
  • 45
81
votes
2 answers

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

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…
raj
  • 811
  • 1
  • 6
  • 3
78
votes
3 answers

StringIO replacement that works with bytes instead of strings?

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.
sorin
  • 161,544
  • 178
  • 535
  • 806
78
votes
3 answers

how do I clear a stringio object?

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?
Incognito
  • 1,883
  • 5
  • 21
  • 28
60
votes
2 answers

What is the best way to write the contents of a StringIO to a file?

What is the best way to write the contents of a StringIO buffer to a file ? I currently do something like: buf = StringIO() fd = open('file.xml', 'w') # populate buf fd.write(buf.getvalue ()) But then buf.getvalue() would make a copy of the…
gauteh
  • 16,435
  • 4
  • 30
  • 34
55
votes
4 answers

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

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 =…
simha
  • 869
  • 1
  • 8
  • 12
52
votes
2 answers

What are the advantages to using StringIO in Ruby as opposed to String?

When is it considered proper to use Ruby's StringIO as opposed to just using String? I think I understand the fundamental difference between them as highlighted by "What is ruby's StringIO class really?", that StringIO enables one to read and write…
Scott Joseph
  • 523
  • 1
  • 4
  • 4
47
votes
4 answers

Do I have to do StringIO.close()?

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…
warvariuc
  • 57,116
  • 41
  • 173
  • 227
47
votes
4 answers

Python Flask send_file StringIO blank files

I want to process a Pandas dataframe and send it to download as a CSV without a temp file. The best way to accomplish this I've seen is to use StringIO. Using the code below, a file downloads with the proper name, however the file is completely…
Daniel Hitchcock
  • 683
  • 1
  • 6
  • 11
41
votes
2 answers

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

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…
Shipra
  • 1,259
  • 2
  • 14
  • 26
41
votes
4 answers

Download and decompress gzipped file in memory?

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 =…
OregonTrail
  • 8,594
  • 7
  • 43
  • 58
1
2 3
23 24