This snippets shows how to have fun replacing multiple words in a text. The target words and the replacement words form key:value pairs in a dictionary. The search and replacement is done using Python's regular expression module re. The code also gives an example of a function within a function.
Multiple Word Replace in Text (Python)
# replace words in a text that match key_strings in a dictionary with the given value_string
# Python's regular expression module re is used here
# tested with Python24 vegaseat 07oct2005
import re
def multiwordReplace(text, wordDic):
"""
take a text and replace words that match a key in a dictionary with
the associated value, return the changed text
"""
rc = re.compile('|'.join(map(re.escape, wordDic)))
def translate(match):
return wordDic[match.group(0)]
return rc.sub(translate, text)
str1 = \
"""When we see a Space Shuttle sitting on its launch pad, there are two big booster rockets
attached to the sides of the main fuel tank. These are solid rocket boosters, made by Thiokol
at their factory in Utah. The engineers who designed the solid rocket boosters might have preferred
to make them a bit fatter, but they had to be shipped by train from the factory to the launch site.
The railroad line from the factory runs through a tunnel in the mountains. The boosters had to fit
through that tunnel. The tunnel is slightly wider than the railroad track. The width of the railroad
track came from the width of horse-drawn wagons in England, which were as wide as two horses' behinds.
So, a major design feature of what is the world's most advanced transportation system was determined
over two thousand years ago by the width of a horse's ###!
"""
# the dictionary has target_word : replacement_word pairs
wordDic = {
'booster': 'rooster',
'rocket': 'pocket',
'solid': 'salted',
'tunnel': 'funnel',
'ship': 'slip'}
# call the function and get the changed text
str2 = multiwordReplace(str1, wordDic)
print str2
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
CS guy 0 Newbie Poster
Skrell 0 Light Poster
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
wallars 0 Newbie Poster
Varunkrishna 0 Junior Poster in Training
Lucaci Andrew 140 Za s|n
paddy3118 11 Light Poster
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
paddy3118 11 Light Poster
ZZucker 342 Practically a Master Poster
Tim1234 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.