How to replace multiple words with one word in pyt

2019-02-26 05:04发布

问题:

This question already has an answer here:

  • How to replace multiple substrings of a string? 19 answers

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", "8 gigabytes", and "8 gbs" should all change to "8 gigabytes"

What would be the best way to do this? Have a seperate replace for each of them?

Also I am trying to do this for more than one word (ie megabytes, terabytes) do each of those need a different replace or is there a way to put them all in one?

回答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'


回答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'