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

fast way to read from StringIO until some byte is encountered

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,…
zaharpopov
  • 16,882
  • 23
  • 75
  • 93
8
votes
3 answers

How can I send a StringIO via FTP in python 3?

I want to upload a text string as a file via FTP. import ftplib from io import StringIO file = StringIO() file.write("aaa") file.seek(0) with ftplib.FTP() as ftp: ftp.connect("192.168.1.104", 2121) ftp.login("ftp", "ftp123") …
user1021531
  • 487
  • 1
  • 7
  • 16
8
votes
4 answers

Serving Excel(xlsx) file to the user for download in Django(Python)

I'm trying create and serve excel files using Django. I have a jar file which gets parameters and produces an excel file according to parameters and it works with no problem. But when i'm trying to get the produced file and serve it to the user for…
Srht
  • 473
  • 3
  • 7
  • 17
7
votes
1 answer

Converting a StringIO object to a Django ImageFile

I'm trying to take data from a StringIO (or cStringIO, more specifically) and convert it to a django.core.files.images.ImageFile. But it doesn't work. Any by that, I mean that it fails in a multitude of ways, and Google has failed me. So far I've…
Brian Hicks
  • 6,213
  • 8
  • 51
  • 77
7
votes
2 answers

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

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…
smci
  • 32,567
  • 20
  • 113
  • 146
7
votes
2 answers

StringIO portability between python2 and python3 when capturing stdout

I have written a python package which I have managed to make fully compatible with both python 2.7 and python 3.4, with one exception that is stumping me so far. The package includes a command line script, and in my unit tests I use this code to run…
NF6X
  • 103
  • 1
  • 4
7
votes
3 answers

Why is StringIO object slower than real file object?

I'm looking through the source of StringIO where it says says some notes: Using a real file is often faster (but less convenient). There's also a much faster implementation in C, called cStringIO, but it's not subclassable. StringIO just like a…
JanuaryStar
  • 103
  • 1
  • 5
7
votes
2 answers

How to pipe binary data into numpy arrays without tmp storage?

There are several similar questions but none of them answers this simple question directly: How can i catch a commands output and stream that content into numpy arrays without creating a temporary string object to read from? So, what I would like to…
K.-Michael Aye
  • 5,465
  • 6
  • 44
  • 56
6
votes
4 answers

Including base64-encoded image in ReportLab-generated PDF

I am trying to decode base64-encoded image and put it into PDF I generate using ReportLab. I currently do it like that (image_data is base64-encoded image, story is already a ReportLab's story): # There is some "story" I append every…
Tadeck
  • 132,510
  • 28
  • 152
  • 198
6
votes
1 answer

How to write UTF-8 CSV into BytesIO in Python3?

Firstly, I understand how to write UTF-8 from strings in Python3 and that StringIO is recommended for such string building. However, I specifically need a binary file-like object and for that I need BytesIO. If I do the following then the data ends…
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
6
votes
1 answer

in-place replacement in StringIO

How do I replace a string with another inside a StringIO? - I've heard it's possible if they're the same length. Attempt: from cStringIO import StringIO c = 'can\nhaz\nfoo' sio = StringIO(c) for line in sio: if line == 'haz\n': #…
A T
  • 13,008
  • 21
  • 97
  • 158
6
votes
2 answers

How can I clear a `StringIO` instance?

How can I clear a StringIO instance? After I write to and read from a string io, I want to clear it. require "stringio" io = StringIO.new io.write("foo") io.string #=> "foo" # ... After doing something ... io.string #=> Expecting "" I tried flush…
sawa
  • 165,429
  • 45
  • 277
  • 381
6
votes
1 answer

python PIL image how to save image to a buffer so can be used later?

I have a png file which should be convert to jpg and save to gridfs , I use python's PIL lib to load the file and do the converting job, the problem is I want to store the converted image to a MongoDB Gridfs, in the saving procedure, I can't just…
armnotstrong
  • 8,605
  • 16
  • 65
  • 130
6
votes
2 answers

Function to create in-memory zip file and return as http response

I am avoiding the creation of files on disk, this is what I have got so far: def get_zip(request): import zipfile, StringIO i = open('picture.jpg', 'rb').read() o = StringIO.StringIO() zf = zipfile.ZipFile(o, mode='w') …
nabucosound
  • 1,283
  • 1
  • 12
  • 23
5
votes
2 answers

Is there a way to make StringIO reading blocking

I've searched through the documentation and searched around but there is nothing said about blocking StringIO objects. I could create my own file-like object that just simply wraps around StringIO but how is the best way to make it blocking? The…
Wessie
  • 3,460
  • 2
  • 13
  • 17