title: How to replace characters in a text by space except for list of words in python
body: <p>I want to replace all characters in a text by spaces, but I want to leave a list of words.</p>
  <p>For instante:</p>
  <pre><code>text = &quot;John Thomas bought 300 shares of Acme Corp. in 2006.&quot;
  list_of_words = ['Acme Corp.', 'John Thomas']
  </code></pre>
  <p>My wanted output would be:</p>
  <pre><code>output_text = &quot;***********                      **********         &quot;
  </code></pre>
  <p>I would like to change unwanted characters to spaces before I do the * replacement:</p>
  <p><code>&quot;John Thomas                      Acme Corp.         &quot;</code></p>
  <p>Right know I know how to replace only the list of words, but cannot come out with the spaces part.</p>
  <pre><code>rep = {key: len(key)*'_**_' for key in list_of_words}
  rep = dict((re.escape(k), v) for k, v in rep.items()) 
  pattern = re.compile(&quot;|&quot;.join(rep.keys()))
  pattern.sub(lambda m: rep[re.escape(m.group(0))], text)
  </code></pre>