Convert StringIO to string

Jonathan Bowlas me at jonbowlas.com
Mon Oct 16 11:52:21 EDT 2006


Your suggestion didn't seem to make any difference at all, it still returns

<script type="text/javascript">
new rss_ticker(gdfgdfg, True, True)
</script>

Any other ideas?

Jon


-----Original Message-----
From: python-list-bounces+info=jonbowlas.com at python.org
[mailto:python-list-bounces+info=jonbowlas.com at python.org] On Behalf Of Rob
Williscroft
Sent: 16 October 2006 14:37
To: python-list at python.org
Subject: Re: Convert StringIO to string

Jonathan Bowlas wrote in
news:mailman.562.1161001625.11739.python-list at python.org in
comp.lang.python: 

> Hi listers,
> 
> I've written this little script to generate some html but I cannot get 
> it to convert to a string so I can perform a replace() on the >, 
> < characters that get returned.
> 
> 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()
> 
> I tried adding this:
> 
> scripter = scripter.replace("<", "<") scripter = 
> scripter.replace(">", ">")
> 
> But obviously replace() isn't an attribute of StringIO so I guess I 
> need to convert it to a string first, can someone please advise how I 
> can do this?
> 

How strange, you are already "converting" to a string in the return line
(the call to the getvalue() method), so:

    	scripter = scripter.getvalue().replace("<", "<")
    	scripter = scripter.replace(">", ">")
    	return scripter

should do what you want.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
--
http://mail.python.org/mailman/listinfo/python-list







More information about the Python-list mailing list