stringio

BytesIO with python v2.5

Question: How do I get a byte stream that works like StringIO for Python 2.5? Application: I'm converting a PDF to text, but don't want to save a file to the hard disk. Other Thoughts: I figured I could use StringIO, but there's no mode parameter (I guess "String" implies text mode). Apparently the io.BytesIO class is new in v2.6,...

How to loop until EOF in Python?

I need to loop until I hit the end of a file-like object, but I'm not finding an "obvious way to do it", which makes me suspect I'm overlooking something, well, obvious. :-) I have a stream (in this case, it's a StringIO object, but I'm curious about the general case as well) which stores an unknown number of records in "<length><data>...

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 datastore as a unicode string so that's fine. The cStringIO.StringIO.writelines function is t...

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 "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call return Popen(*popenargs,...

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') zf.writestr('picture.jpg', i) zf.close() o.seek(0) response = HttpResponse(o.read()) ...

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 buf to a file ? I currently do something like: buf = StringIO() fd = open ('file.xml', 'w') # populate buf fd.write (buf.getvalue ()) But then all of buf would be loaded in memory at the same time.. ? ...

help with handling tokenization errors

Hi, The code is in python. Please find below the piece of code that I use to tokenize a string. strList = list(token[STRING] for token in generate_tokens(StringIO(line).readline) if token[STRING]) I get an error that reads like:- raise TokenError, ("EOF in multi-line statement", (lnum, 0)) tokenize.TokenError: ('EOF in multi-li...

python 2.7 / exec / what is wrong?

Hello, 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 = StringIO() sys.stdout = sys.stderr = sio try: exec(stmt, globals()...

Unicode problems when using io.StringIO to mock a file

I am using an io.StringIO object to mock a file in a unit-test for a class. The problem is that this class seems expect all strings to be unicode by default, but the builtin str does not return unicode strings: >>> buffer = io.StringIO() >>> buffer.write(str((1, 2))) TypeError: can't write str to text stream But >>> buffer.write(str(...

Create Python array.array Object from cStringIO Object

I want to create an array.array object from a cStringIO object: import cStringIO, array s = """ <several lines of text> """ f = cStringIO.StringIO(s) a = array.array('c') a.fromfile(f, len(s)) But I get the following exception: Traceback (most recent call last): File "./myfile.py", line 22, in <modu...

Python's StringIO for Clojure

Hi, Is there something equivalent to Python's StingIO for Clojure? I'm trying to write a report generating/literate programming system similar to Sweave and Pweave for Clojure. I'm currently using a temp file, but I'd prefer using something similar to StringIO. ...

How to make cStringIO transparent to another function that expects a real local file.

Hello everybody! I came up with the following problem: CODE A works right now.. I am saving a png file called chart.png locally, and then I am loading it into the proprietary function (which I do not have access). However, in CODE B, am trying to use cStringIO.StringIO() so that I do not have to write the file "chart.png" to the disk. ...