用字符串中的单词替换单词(Replace a word by a word in a string)

系统教程 行业动态 更新时间:2024-06-14 16:57:17
用字符串中的单词替换单词(Replace a word by a word in a string)

我有一个像下面的字典

word_dict = {'a': 'a1', 'winter': 'cold', 'summer': 'hot'}

我有这样一个字符串:

data = "It's winter not summer. Have a nice day"

我想要做的是在data a by a1代替a by a1这个词, winter by cold等等。 我确实尝试使用下面的代码:

for word in word_dict: data = data.replace(word, word_dict[word])

但它失败了,因为它取代了子串( data的子串,而不是单词)。 事实上,这个词Have被Ha1ve取代。

结果应该是:

data = "It's cold not hot. Have a1 nice day"

I have a dictionary like below

word_dict = {'a': 'a1', 'winter': 'cold', 'summer': 'hot'}

and I have a string like this:

data = "It's winter not summer. Have a nice day"

What I want to do is to replace the word a by a1, winter by cold, etc in the data. I did try to use the below code:

for word in word_dict: data = data.replace(word, word_dict[word])

But it fails because it replaces the substring (substring of the data, not the word). Infact, the word Have is replace by Ha1ve.

The result should be:

data = "It's cold not hot. Have a1 nice day"

最满意答案

你可以使用re.sub 。 \b在单词字符和非单词字符之间匹配的单词边界。 我们需要使用单词边界来匹配一个确切的单词字符串,否则,它会匹配day的a

>>> word_dict = {'a': 'a1', 'winter': 'cold', 'summer': 'hot'} >>> data = "It's winter not summer. Have a nice day" >>> for word in word_dict: data = re.sub(r'\b'+word+r'\b', word_dict[word], data) >>> data "It's cold not hot. Have a1 nice day"

You could use re.sub . \b word boundary which matches between a word character and a non-word character. We need to use word boundary to match an exact word string or otherwise, it would match also the a in day

>>> word_dict = {'a': 'a1', 'winter': 'cold', 'summer': 'hot'} >>> data = "It's winter not summer. Have a nice day" >>> for word in word_dict: data = re.sub(r'\b'+word+r'\b', word_dict[word], data) >>> data "It's cold not hot. Have a1 nice day"

更多推荐

本文发布于:2023-04-12 19:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4e4c2644ac678096a5067109be6b53cf.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   用字   符串中   string   Replace

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!