RE: Replacing "illegal characters" in html
From: Robert Brewer (fumanchu_at_amor.org)
Date: 05/09/04
- Next message: John Hunter: "emacs alphabetize methods"
- Previous message: Robert Brewer: "RE: Replacing "illegal characters" in html"
- Maybe in reply to: BenO: "Replacing "illegal characters" in html"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: John Hunter: "emacs alphabetize methods"
- Previous message: Robert Brewer: "RE: Replacing "illegal characters" in html"
- Maybe in reply to: BenO: "Replacing "illegal characters" in html"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|