replacing characters within a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    replacing characters within a string

    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 <audioentry"

    and I want to replace <audiowith &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.
  • Chris Rebert

    #2
    Re: replacing characters within a string

    On Sat, Nov 8, 2008 at 9:16 PM, John Smith <swader@gmail.c omwrote:
    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 -
    This module is no longer part of the Python standard library. It was removed in Python 3.13 after being deprecated in Python 3.11. The removal was decided in PEP 594. A fork of the module on PyPI c...


    Cheers,
    Chris
    --
    Follow the path of the Iguana...

    >
    example csv file row:
    "FIXED","All"," Enable <audioentry"
    >
    and I want to replace <audiowith &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.
    --

    >

    Comment

    • Terry Reedy

      #3
      Re: replacing characters within a string

      Chris Rebert wrote:
      On Sat, Nov 8, 2008 at 9:16 PM, John Smith <swader@gmail.c omwrote:
      >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;".
      FYI: Python strings do have a .replace method,
      but for the above application
      You want the cgi.escape() function -
      http://docs.python.org/library/cgi.html#cgi.escape
      *is* what you should use, as it will do all needed replacements in one call.

      Comment

      Working...