How To Replace Multiple Words With One Word In Python?
I have a few strings that may contain the abbreviation or the full name of something and I would like to replace them all to the same variation of the word. For example, '8 gigs',
Solution 1:
A simple re.sub
will do the trick for you.
>>>import re>>>s = 'gigabytes, foo gigs; foo gbs'>>>re.sub('(gigabytes|gigs|gbs)','gigabytes',s)
'gigabytes, foo gigabytes; foo gigabytes'
Solution 2:
>>>import re>>>re.sub(r'(\d+) (gigs|gigabytes|gbs)', r'\1 gigabytes', str)
for multiple substitutions, the trick is to use a callable (in this case a lambda function) as replacement:
>>> gb='gigabytes'>>> mb='megabytes'>>> subs={'gigs': gb, 'gigabytes': gb, 'gbs': gb, 'mbs': mb, ...}
>>> str='there are 2048 mbs in 2 gigs'>>> re.sub(r'(\d+) ({})'.format('|'.join(subs.keys())), \
lambda x: '{} {}'.format(x.group(1), subs[x.group(2)]), str)
'there are 2048 megabytes in 2 gigabytes'
Post a Comment for "How To Replace Multiple Words With One Word In Python?"