I am trying to remove a list of words from a string using python. I tried the code below, but it is adding space while replacing words in the string. Is there any approach which helps to remove only the word present in the list of words? Please give me some advice.
words_to_remove=['gosh', 'no', 'oh', 'Yep', 'ow', 'well', 'goodness', 'Yeah']
test_data = """RegExr Yeah was created by gskinner.com.
yippe, ow, ouch, gosh Yeah oh, goodness, oh well, oh no, how can I do wonders in this world. Yep, it is out of the world.
Edit the Expression & Text, to-see matches. Roll; over$ matches% or* the expr@ession for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode.
The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community and view patterns you create or favorite in My Patterns.
Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.
"""
# Remove words
for word in words_to_remove:
test_data = test_data.replace(word, '')
test_data
Out[46]: 'RegExr was created by gskinner.com.\nyippe, , ouch, , , , , h can I do wonders in this world. , it is out of the world.\nEdit the Expression & Text, to-see matches. Roll; over$ matches% or* the expr@ession for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode.\nThe side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community and view patterns you create or favorite in My Patterns.\nExplore results with the Tools bel. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.\n'