Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Strings in Python are <em>immutable</em> (can't be changed). Because of this, the effect of <code>line.replace(...)</code> is just to create a new string, rather than changing the old one. You need to <em>rebind</em> (assign) it to <code>line</code> in order to have that variable take the new value, with those characters removed.</p> <p>Also, the way you are doing it is going to be kind of slow, relatively. It's also likely to be a bit confusing to experienced pythonators, who will see a doubly-nested structure and think for a moment that something more complicated is going on.</p> <p>Starting in Python 2.6 and newer Python 2.x versions *, you can instead use <a href="https://docs.python.org/2/library/stdtypes.html#str.translate" rel="noreferrer"><code>str.translate</code></a>, (but read on for Python 3 differences):</p> <pre><code>line = line.translate(None, '!@#$') </code></pre> <p>or regular expression replacement with <a href="https://docs.python.org/2/library/re.html#re.sub" rel="noreferrer"><code>re.sub</code></a></p> <pre><code>import re line = re.sub('[!@#$]', '', line) </code></pre> <p>The characters enclosed in brackets constitute a <em>character class</em>. Any characters in <code>line</code> which are in that class are replaced with the second parameter to <code>sub</code>: an empty string.</p> <p>In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a <a href="https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python#comment-4205256">comment</a> on one of the answers, and it's noted in the <a href="https://docs.python.org/2/library/stdtypes.html#str.translate" rel="noreferrer">documentation for <code>str.translate</code></a>.</p> <p>When calling the <code>translate</code> method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass <code>None</code> as the first parameter, or even a translation table from <code>string.maketrans</code>. Instead, you pass a dictionary as the only parameter. This dictionary maps the <em>ordinal values</em> of characters (i.e. the result of calling <a href="https://docs.python.org/2/library/functions.html#ord" rel="noreferrer"><code>ord</code></a> on them) to the ordinal values of the characters which should replace them, or—usefully to us—<code>None</code> to indicate that they should be deleted.</p> <p>So to do the above dance with a Unicode string you would call something like</p> <pre><code>translation_table = dict.fromkeys(map(ord, '!@#$'), None) unicode_line = unicode_line.translate(translation_table) </code></pre> <p>Here <a href="https://docs.python.org/2/library/stdtypes.html#dict.fromkeys" rel="noreferrer"><code>dict.fromkeys</code></a> and <a href="https://docs.python.org/2/library/functions.html#map" rel="noreferrer"><code>map</code></a> are used to succinctly generate a dictionary containing</p> <pre><code>{ord('!'): None, ord('@'): None, ...} </code></pre> <p>Even simpler, as <a href="https://stackoverflow.com/questions/23175809/typeerror-translate-takes-one-argument-2-given-python">another answer puts it</a>, create the dictionary in place:</p> <pre><code>unicode_line = unicode_line.translate({ord(c): None for c in '!@#$'}) </code></pre> <hr> <p>* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of <code>None</code>:</p> <pre><code>import string line = line.translate(string.maketrans('', ''), '!@#$') </code></pre> <p>Here <a href="https://docs.python.org/2/library/string.html#string.maketrans" rel="noreferrer"><code>string.maketrans</code></a> is used to create a <em>translation table</em>, which is just a string containing the characters with ordinal values 0 to 255.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload