如何在python中用一个词替换多个词? [英] How to replace multiple words with one word in python?

查看:71
本文介绍了如何在python中用一个词替换多个词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些字符串可能包含某事物的缩写或全名,我想将它们全部替换为单词的相同变体.

例如,

8 场演出",8 GB",以及8GB"都应该改为8GB"

这样做的最佳方法是什么?每个人都有单独的替代品吗?

此外,我试图为多个词(即兆字节、太字节)执行此操作,每个词都需要不同的替换,还是有办法将它们全部放在一起?

解决方案

>>>进口重新>>>re.sub(r'(\d+) (gigs|gigabytes|gbs)', r'\1 GB', str)

对于多个替换,诀窍是使用可调用(在本例中为 lambda 函数)作为替换:

<预><代码>>>>gb='千兆字节'>>>mb='兆字节'>>>subs={'gigs': gb, 'gigabytes': gb, 'gbs': gb, 'mbs': mb, ...}>>>str='2 演出中有 2048 mbs'>>>re.sub(r'(\d+) ({})'.format('|'.join(subs.keys())), \lambda x: '{} {}'.format(x.group(1), subs[x.group(2)]), str)'2 GB 中有 2048 MB'

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?

解决方案

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

这篇关于如何在python中用一个词替换多个词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆