Python - removing some punctuation from text
<p>I want Python to remove only some punctuation from a string, let's say I want to remove all the punctuation except '@'</p>

<pre><code>import string
remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))
sample = 'The quick brown fox, like, totally jumped, @man!'
sample.translate(remove)
</code></pre>

<p>Here the output is </p>

<pre><code>The quick brown fox like totally jumped man
</code></pre>

<p>But what I want is something like this </p>

<pre><code>The quick brown fox like totally jumped @man
</code></pre>

<p>Is there a way to selectively remove punctuation from a text leaving out the punctuation that we want in the text intact?</p>

<p><a href="https://docs.python.org/2/library/string.html#string.punctuation" rel="nofollow">str.punctuation</a> contains all the punctuations. Remove <code>@</code> from it. Then replace with <code>''</code> whenever you get that punctuation string. </p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; a = string.punctuation.replace('@','')
&gt;&gt;&gt; re.sub(r'[{}]'.format(a),'','The quick brown fox, like, totally jumped, @man!')
'The quick brown fox like totally jumped @man'
</code></pre>

python