我有一个像下面的字典
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"更多推荐
发布评论