Convert StringIO to string

Fredrik Lundh fredrik at pythonware.com
Mon Oct 16 12:07:32 EDT 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(www.bbc.co.uk, 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?

</F>




More information about the Python-list mailing list