convert string to input stream

T

Titus Barik

Hi,

I've used Google quite a bit in an attempt to find an answer to this
question, but have found no leads, possibly because I'm not sure I'm
wording my question properly. So I thought I'd try here.

Basically I have a function that accepts an input file stream as an
argument:

handle = open(SOME_FILE)
my_function(handle)

But occasionally, I end up with a string ("Hello world") that I need to
convert to a stream in order to pass it into this function. Perhaps
something like:

fake_handle = convert_to_handle("Hello world!")

so that it appears to this function to be a file handle. I've seen
functions in other languages that will take strings and convert them to
streams via IO Memory Buffers and such, but can't seem to find anything in
Python; or at least I don't know where to look.

One solution that I didn't like was to do the following:

1. Open a temporary file.
2. Write the string to that file.
3. Read it back.

Any suggestions are appreciated.

Regards,
 
J

Jeremy Jones

Titus said:
Hi,

I've used Google quite a bit in an attempt to find an answer to this
question, but have found no leads, possibly because I'm not sure I'm
wording my question properly. So I thought I'd try here.

Basically I have a function that accepts an input file stream as an
argument:

handle = open(SOME_FILE)
my_function(handle)

But occasionally, I end up with a string ("Hello world") that I need to
convert to a stream in order to pass it into this function. Perhaps
something like:

fake_handle = convert_to_handle("Hello world!")
What you want is StringIO:
http://www.python.org/doc/current/lib/module-StringIO.html
or cStringIO:
http://www.python.org/doc/current/lib/module-cStringIO.html

I'll leave usage between you and the pydocs (unless, of course, you try
it and have some problems with it).
so that it appears to this function to be a file handle. I've seen
functions in other languages that will take strings and convert them to
streams via IO Memory Buffers and such, but can't seem to find anything in
Python; or at least I don't know where to look.

One solution that I didn't like was to do the following:

1. Open a temporary file.
2. Write the string to that file.
3. Read it back.
Yeah - that's a nasty answer. You don't wanna go there.
 
A

Alex Martelli

Titus Barik said:
handle = open(SOME_FILE)
my_function(handle)

But occasionally, I end up with a string ("Hello world") that I need to
convert to a stream in order to pass it into this function. Perhaps
something like:

fake_handle = convert_to_handle("Hello world!")

so that it appears to this function to be a file handle. I've seen

def convert_to_handle(somestring):
import cStringIO
return cStringIO.StringIO(somestring)


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top