Convert StringIO to string

Fredrik Lundh fredrik at
Mon Oct 16 18:07:32 CEST 2006

Jonathan Bowlas wrote:

> Ok, I think I'm explaining this badly because I've used getvalue() in 
> the script I originally submitted to the list, see below:
> from StringIO import StringIO
> def generator_file(rsspath,titleintro,tickeropt):
>       scripter=StringIO()
>       scripter.write('<script type="text/javascript">\n')
>       scripter.write('new rss_ticker(%s, %s, %s)\n' % (rsspath, 
> titleintro, tickeropt))
>       scripter.write('</script>\n')
>       return scripter.getvalue()
> The problem is this script returns the following:
> <script type="text/javascript">
> new rss_ticker(, True, True) </script>

no, it doesn't.  it returns "<script" etc.  as a string.  because that's 
what getvalue() does.

if you end up getting < in the output, it's because you're passing 
the output from generator_file(...) to some other part of your program 
that does HTML escaping.  StringIO has nothing to do with that, and no 
amount of replacing inside generator_file() will change that.

> So what am I doing wrong?

coding under the influence of confusion?

can you post the code that calls generator_file() and the code that 
writes the return value to the browser?


More information about the Python-list mailing list