replacing characters within a string

J

John Smith

Hi,

I coded a python script that lets me parse a csv file into html code
with a certain format, but I would like to replace every "<" and ">"
character that appears within each column entry of the csv file (they
are parsed as strings) with the html equivalents of "&lt;" and "&gt;".

example csv file row:
"FIXED","All","Enable <audio> entry"

and I want to replace <audio> with &lt;audio&gt; before I do a

"<p>This is a " + str(array[0]) + " bug that applies to " +
str(array[1]) + " platforms and has the description: " + str(array[2]) +
".</p>"

to get the following html code:

<p>This is a FIXED bug that applies to All platforms and has the
description: Enable &lt;audio&gt; entry.</p>

(sometimes < appears twice or thrice and sometimes only > appears so
they are not necessarily appearing together, e.g. -> instead of <something>)

How should I go about doing it?

Regards.
 
C

Chris Rebert

Hi,

I coded a python script that lets me parse a csv file into html code with a
certain format, but I would like to replace every "<" and ">" character that
appears within each column entry of the csv file (they are parsed as
strings) with the html equivalents of "&lt;" and "&gt;".

You want the cgi.escape() function -
http://docs.python.org/library/cgi.html#cgi.escape

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
example csv file row:
"FIXED","All","Enable <audio> entry"

and I want to replace <audio> with &lt;audio&gt; before I do a

"<p>This is a " + str(array[0]) + " bug that applies to " + str(array[1]) +
" platforms and has the description: " + str(array[2]) + ".</p>"

to get the following html code:

<p>This is a FIXED bug that applies to All platforms and has the
description: Enable &lt;audio&gt; entry.</p>

(sometimes < appears twice or thrice and sometimes only > appears so they
are not necessarily appearing together, e.g. -> instead of <something>)

How should I go about doing it?

Regards.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top