user1403546
user1403546

Reputation: 1749

Python - replace all words in a string except some

Given a text like

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ac massa arcu.

I want to write a function that replaces all the words in a text, except some defined in a list keep_list, with a given string xxxx. So, if

keep_list = ['amet', 'ac']

I'd like to have an output like

xxxx xxxx xxxx xxxx amet, xxxx xxxx xxxx. xxxx ac xxxx xxxx.

I want to keep all commas, points etc. of the original string and this is the only difficulty of this function.

How would you do it in an elegant way?

Upvotes: 0

Views: 1708

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97150

One approach would be to use a regular expression to match single words, and then pass a callback to re.sub() to replace with xxxx if the word is not in the list.

import re

txt = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ac massa arcu.'
keep = ['amet', 'ac']

print(re.sub(r'\b\w+\b', lambda w: w.group() if w.group() in keep else 'xxxx', txt))

Upvotes: 5

Related Questions