Replacing parts of strings in a list in Python

doxyl

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.

falsetru

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replacing white space inside a list of strings in python

From Dev

Swap parts of a list in python

From Dev

Replacing specific occurrences of strings in Python

From Dev

Replacing specific occurrences of strings in Python

From Dev

Python replacing numbers in a list

From Dev

Python - Replacing characters in a list

From Dev

replacing the values of a list in python

From Dev

How can I replace parts of strings in a list?

From Dev

Changing the values in multiple parts of a grid (list of strings?)

From Dev

Sort list of strings by order of numeric parts

From Dev

Referring to parts of a list for a calculation (Python)

From Dev

Create tree of parts of strings out of list of systematical strings (filenames)

From Dev

python single quotes around certain parts of strings

From Dev

Replacing Values in a List with Index in Python

From Dev

Replacing duplicate elements in a list in Python?

From Dev

Replacing python list elements with key

From Dev

Replacing elements in long list Python

From Dev

Cost of replacing an element of a list in python

From Dev

Replacing part of a string in a list in python

From Dev

Python iterating through dictionaries replacing strings

From Dev

Python join parts of a string list together

From Dev

Python export only parts of list to CSV

From Dev

Replacing strings with strings in Java

From Dev

Python Remove List of Strings from List of Strings

From Dev

How could I easily expand a list of numbers with hyphens replacing repeated parts?

From Dev

Column with list of strings in python

From Dev

given a list of strings in python

From Dev

Convert strings to list Python

From Dev

Filtering a list of strings in python

Related Related

HotTag

Archive