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
5
votes
3 answers

StringIO with binary files?

I seem to get different outputs: from StringIO import * file = open('1.bmp', 'r') print file.read(), '\n' print StringIO(file.read()).getvalue() Why? Is it because StringIO only supports text strings or something?
Joelmc
  • 873
  • 2
  • 8
  • 5
5
votes
2 answers

lxml.etree.iterparse closes input file handler?

filterous is using iterparse to parse a simple XML StringIO object in a unit test. However, when trying to access the StringIO object afterwards, Python exits with a "ValueError: I/O operation on closed file" message. According to the iterparse…
l0b0
  • 55,365
  • 30
  • 138
  • 223
5
votes
2 answers

Numpy array from cStringIO object and avoiding copies

This to understand things better. It is not an actual problem that I need to fix. A cstringIO object is supposed to emulate a string, file and also an iterator over the lines. Does it also emulate a buffer ? In anycase ideally one should be able to…
san
  • 4,144
  • 6
  • 32
  • 50
5
votes
5 answers

Storing image using open URI and paperclip having size less than 10kb

I want to import some icons from my old site. The size of those icons is less than 10kb. So when I am trying to import the icons its returning stringio.txt file. require "open-uri" class Category < ActiveRecord::Base has_attached_file :icon, …
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
5
votes
1 answer

What are the differences between tempfile module and IO file-like objects

I have found that there are a lot of similarities between both modules in the area of creating temp files using io.BytesIO() or io.StringIo() and tempfile.TemporaryFile() What is the purpose of each one ?
Mohamed Samir
  • 367
  • 3
  • 9
5
votes
3 answers

Create image from RGB list with Pillow and Python 3

I have a list of RGB data: cdata=[R1, G1, B1, R2, G2, B2,..., Rn, Gn, Bn] where each value is comprised between 0 to 255. I am trying to rebuild this array as an image using Pillow 5.0.0. Under Python 2, I was able to convert the list of values…
Zebulon
  • 53
  • 1
  • 1
  • 4
5
votes
1 answer

Python decompress gzip data in memory without file

I have gzipped data from HTTP reply. I have following code: def gzipDecode(self, content): import StringIO import gzip outFilePath = 'test' compressedFile = StringIO.StringIO(content) decompressedFile =…
martin
  • 1,707
  • 6
  • 34
  • 62
5
votes
2 answers

python 2.7 / exec / what is wrong?

I have this code which runs fine in Python 2.5 but not in 2.7: import sys import traceback try: from io import StringIO except: from StringIO import StringIO def CaptureExec(stmt): oldio = (sys.stdin, sys.stdout, sys.stderr) sio =…
Elias Bachaalany
  • 1,180
  • 2
  • 13
  • 27
5
votes
0 answers

PDF does not contain EOF marker (PDF::Reader::MalformedPDFError) with pdf-reader

I am using ‘pdf-reader’ gem to read raw contents of pdf documents so I can post (http-post) them to an API. To confirm the API implementation can create a valid pdf document from the raw content, I wrote a small gist to validate my code to read the…
Som Poddar
  • 1,428
  • 1
  • 15
  • 22
5
votes
0 answers

How to put a new line using StringIO in python?

import StringIO output = StringIO.StringIO() output.write('this is the first line\n the new line should come here') a = output.getvalue() output.close() print a But the new line character \n does not get printed as a new line. How do I get a…
user3676102
  • 51
  • 1
  • 2
5
votes
1 answer

Ruby Mock a file with StringIO

I am trying to mock file read with the help of StringIO in Ruby. The following is my test and next to that is my method in the main class. def test_get_symbols_from_StringIO_file s = StringIO.new("YHOO,141414") assert_equal(["YHOO,141414"],…
5
votes
1 answer

scikit-image save image to a bytestring

I'm using scikit-image to read an image: img = skimage.io.imread(filename) After doing some manipulations to img, I'd like to save it to an in-memory file (a la StringIO) to pass off to another function, but it looks like skimage.io.imsave requires…
Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
5
votes
3 answers

python sys.stdout and C++ iostreams::cout

I was assuming that sys.stdout would be referencing the same physical stream as iostreams::cout running in the same process, but this doesn't seem to be the case. The following code, which makes a call to a C++ function with a python wrapper called…
doetoe
  • 765
  • 7
  • 16
4
votes
4 answers

Is Python cStringIO thread-safe?

As title say, does Python cStringIO protect their internal structures for multithreading use? Thank you.
Emilio
  • 3,901
  • 11
  • 44
  • 50
4
votes
1 answer

Python: simulate writing to a file object without creating a file

I'm working with Python3 and I want to simulate writing to a file, but without actually creating a file. For example, my specific case is as follows: merger = PdfFileMerger() for pdf in files_to_merge: …
Xar
  • 7,572
  • 19
  • 56
  • 80