I know similar questions exist for this topic but I've gone through them and still couldn't get it.
My python program retrieves a subsection of html from a page using a regular expression. I just realised that I hadn't accounted for html special characters getting in the way.
say I have:
regex_title = ['I went to the store', 'Itlt's a nice day today', 'I went home for a rest']
I obviously want to change lt'
to a single quote '.
I've tried variations of:
for each in regex_title:
if 'lt'' in regex_title:
str.replace("lt'", "'")
but had no success. What am I missing.
NOTE: The purpose is to do this without importing any more modules.
str.replace
does not replace in-place. It returns the replaced string. You need to assigned back the return value.
>>> regex_title = ['I went to the store', 'Itlt's a nice day today',
... 'I went home for a rest']
>>> regex_title = [s.replace("lt'", "'") for s in regex_title]
>>> regex_title
['I went to the store', "It's a nice day today", 'I went home for a rest']
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments