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
1
vote
1 answer

unicodecsv.DictReader not working with io.StringIO (Python 2.7)

I was trying to use csv.DictReader to parse UTF-8 data with special characters but I was getting the following error: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe3' in position 2: ordinal not in range(128) I read online and found…
Ariel
  • 3,383
  • 4
  • 43
  • 58
1
vote
1 answer

Django/Python: CSV for-in loop overriding first row each time through

class CSVDownload(View): """ Prepares CSV file version to download """ #more code here f = StringIO.StringIO() writer = csv.writer(f, dialect='excel') for v in visit_list: …
ApathyBear
  • 9,057
  • 14
  • 56
  • 90
1
vote
1 answer

Python, how to create in-memory zip file whose files contained in it could have any format (.txt, .jpg, etc.)

I'm attempting to make a class which create a .zip in memory whose content could be any file with any format to use it later. I found useful code and built this class: import zipfile import StringIO class InMemoryZip(object): def…
fenix688
  • 2,435
  • 2
  • 17
  • 23
1
vote
1 answer

Open Image From Online, Save To Server Flask

I am using Flask to open an image from a URL. file = cStringIO.StringIO(urllib.urlopen(URL).read()) img = Image.open(file) I then want to take the image and save it to my site. When I do this, I get Traceback (most recent call last): File…
user3822146
  • 114
  • 3
  • 10
1
vote
1 answer

PIL.Image.open() gives IOError: cannot identify image file

I'm trying to save a jpg image from a Flask app form. The following code works fine: blob = request.files[canvas_key] blob.stream.seek(0) data = blob.stream.read() string_io = cStringIO.StringIO(data) string_io has type
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
1
vote
1 answer

Create Zipfile in Python in memory on Web Server

I'm working on a HTML WYSIWYG editor, and I am currently working on a 'Download' feature where the user can press a Download button to download a zip file of their theme. I am using a Python CGI script to achieve this feature. Currently, my script…
Rashad
  • 235
  • 4
  • 15
1
vote
1 answer

Python3: StringIO and Elementree encoding

With migration to Python 3 xml.etree.ElementTree's write() doesn't work as expected. I need to make the following code work with Python3: tree = ET.ElementTree(root) fileobj = StringIO() tree.write(fileobj, encoding="utf-8") The problem with py3…
Nikolay Derkach
  • 1,734
  • 2
  • 22
  • 34
1
vote
1 answer

"I/O operation on closed file" with StringIO in Django view test

I inherited the following Django view code, used by another webservice to serve downloadable versions of output data: def index(request): # ... (snip) ... data = base64.decodestring(request.POST['data']) filename =…
supervacuo
  • 9,072
  • 2
  • 44
  • 61
1
vote
1 answer

Rezise an image before uploading it with django

I would like to resize an image before uploading to reduce it weight. I use python 3.3 and django 1.5. I read about io.StringIO : I don't understand the answer of this post : Django resize image during upload I don't understand io.StringIO even…
HydrUra
  • 1,336
  • 2
  • 13
  • 23
1
vote
1 answer

Read excel file from StringIO buffer to dataframe with pandas.io.parsers.ExcelFile?

I'd like to read a string buffer into a pandas DataFrame. It seems that a good way to do it would be to use pandas' ExcelFile functionality. I've tried to do something like the following: from pandas import ExcelFile as excel_handler excel_data =…
Lamps1829
  • 2,231
  • 3
  • 24
  • 32
1
vote
1 answer

In Memory Zip File Python Error

I'm trying to make an in-memory zip file in Python and upload it to Amazon S3. I've read the similar posts on the matter, but no matter what I try, Windows and Linux (RHEL5) cannot open it (it's corrupt). Here's the code I'm running: f_redirects =…
Matt Stern
  • 717
  • 11
  • 23
1
vote
2 answers

StringWriter/StringIO for Perl

I'm trying to write some unit tests for some communications code that writes to a socket. During testing I'd like to have my communications library write to a string, then I can compare the contents of the string to what I expect to be written. In…
Bryan Kyle
  • 13,361
  • 4
  • 40
  • 45
1
vote
0 answers

How to empty the buffer stringIO in python

Actually i am trying to export a csv file with some data and below is my code import csv from cStringIO import StringIO import web def download(list__of_records): csv_file = StringIO() csv_writer = csv.writer(csv_file) …
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
1
vote
3 answers

unzipping a zip archive from a string

I have a zip archive in a string, but the rubyzip gem appears to want input from a file. The best I've come up with is to write the zip archive to a tempfile for the sole purpose of passing the filename to Zip::ZipFile.foreach(), but this seems…
fearless_fool
  • 33,645
  • 23
  • 135
  • 217
1
vote
1 answer

How to strip password encryption from zipfile with Python

I have an use case where I need to strip decryption of password protected zip file attached to an email, and replace it with the same zip file unencrypted. What I have so far: import zipfile import StringIO ... if part.get_content_type() ==…
Fuu
  • 3,424
  • 4
  • 33
  • 49