473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Convert StringIO to string

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("&lt;", "<")
scripter = scripter.replace("&gt;", ">")

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?

Cheers

Jon
Oct 16 '06 #1
2 26091
Jonathan Bowlas wrote:
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?
StringIO objects are file-like objects, so you need to use read or
readlines to get the string data out of it (just like a regular file).
Before reading remember to seek back to the beginning to get all of the
data ("Be kind, rewind!"):
>>import StringIO
s = StringIO.StringIO()
s.write("hello world\n")
s.seek(0)
s.read()
'hello world\n'
>>s = StringIO.StringIO()
s.write("hello world\n")
s.read()
''

Oct 16 '06 #2
Jonathan Bowlas wrote in
news:ma**************************************@pyth on.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 &gt;,
&lt; 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("&lt;", "<")
scripter = scripter.replace("&gt;", ">")

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("&lt;", "<")
scripter = scripter.replace("&gt;", ">")
return scripter

should do what you want.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Oct 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric | last post by:
Guys, I have the following hexadecimal string - '\xff\xff\xff' which i need to convert to binary. How would i go about doing this? Eric
10
by: Jochen Hub | last post by:
Hi, I am a real beginner in Python and I was wondering if there is a way to convert a string (which contains a list) to a "real" list. I need this since I would like to give a list as an...
7
by: Matthew Thorley | last post by:
I'm writing a web app whereby a user uploads a tar acrhive which is then opened and processed. My web form reads the file like this: while 1: data = value.file.read(1024 * 8) # Read blocks of...
21
by: Paul Rubin | last post by:
I've always found the string-building idiom temp_list = for x in various_pieces_of_output(): v = go_figure_out_some_string() temp_list.append(v) final_string = ''.join(temp_list) ...
13
by: diffuser78 | last post by:
I want to print number 0 to 9 in one line like this 0 1 2 3 4 5 6 7 8 9 if I do like this, it prints in different lines for i in xrange(10): print i so i tried like this
3
by: Laszlo Nagy | last post by:
This program: import sys import traceback import cStringIO a = 1.0 b = 0.0 try: c=a/b
23
by: Carl K | last post by:
I need to take the take the pdf output from reportlab and create a preview image for a web page. so png or something. I am sure ghostscript will be involved. I am guessing PIL or ImageMagic ? ...
2
by: Bart Kastermans | last post by:
I have written a little program that takes as input a text file, converts it to a list with appropriate html coding (making it into a nice table). Finally I want to upload this list as a textfile...
7
by: neeru29 | last post by:
I have new to python programming. I have sucessfully converted a dictionary into a string. but i want to know how can convert the string back to the dictionary. let us say that 'd' is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.