RE: Replacing "illegal characters" in html

From: Robert Brewer (fumanchu_at_amor.org)
Date: 05/09/04


Date: Sun, 9 May 2004 13:08:52 -0700
To: "BenO" <ben.nospam@ladeda.co.uk>, <python-list@python.org>

BenO wrote:
> I'm new to python and need to write a function to replace
> certain characters
> in a string (html).
>
> The characters I need to replace come from MS Word copy &
> paste and are:
>
> ' (Left quote)
> ' (Right quote)
> Double Left quotes
> Double Right quotes
>
> Can anyone help me or point me in the right direction on an
> efficient way of doing this?

The two methods most often used are 1) the .replace method of strings,
and 2) regular expressions.

1) The .replace method:

>>> replacemap = {""": '"', """: '"', "'": "'", "'": "'"}
>>> map(ord, replacemap.keys())
[145, 147, 146, 148]
>>> test = ""hl" 'oh'"
>>> for k, v in replacemap.iteritems():
... test = test.replace(k, v)
...
>>> test
'"hl" \'oh\''

2) Regular Expressions:

>>> import re
>>> test = ""hl" 'oh'"
>>> test = re.sub("[""]", '"', test)
>>> test = re.sub("['']", "'", test)
>>> test
'"hl" \'oh\''

Hope that helps!

Robert Brewer
MIS
Amor Ministries
fumanchu@amor.org



Relevant Pages

  • Re: Get text "literally" from a TextBox
    ... Cor and Patrice, thanks for the answer; I know the regular expressions, but ... my problem is how get the pattern string if the user put that in a Textbox. ... maybe I can depure my string, but exist another especial "characters" like ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Open, Close, & Freefile
    ... statement adds characters such as quotes around string data. ... Write #fnum, "This is some text..." ... VB writes out the quotes whether a literal or in a variable. ... It is imo on the same line as using the trailing "$" on string intrinsics to ensure the character version is returned as opposed to Variant when that is what is intended, etc., ... ...
    (microsoft.public.vb.general.discussion)
  • Mandis Quotes (aka retiring """ and )
    ... arbitrary textual matter called "Mandis quotes". ... surround the string by a pair of doubled single quotes. ... of ASCII or Unicode characters, but instead as a sequence of lines ...
    (comp.lang.python)
  • Re: Split with regular expressions
    ... > I have a question concerning regular expressions: ... > i have the following example string including quotas: ... it looks like you also want to strip the quotes themselves. ...
    (comp.lang.java.programmer)
  • Re: JavaScript to validate User input
    ... I need to write a Java Script for a string payment_code which comes ... If a user enters characters other than the mentioned above, ... Calulate the length of the string variable ls_tmp_string and store ... Or buy the great book 'Mastering regular expressions' by O'Reilly. ...
    (comp.lang.javascript)