patternpythonCriticalCanonical
Remove specific characters from a string in Python
Viewed 0 times
fromremovecharactersspecificstringpython
Problem
I'm trying to remove specific characters from a string using Python. This is the code I'm using right now. Unfortunately, it appears to do nothing to the string.
How do I do this properly?
See Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string? for the specific debugging question about what is wrong with this approach. Answers here mainly focus on how to solve the problem.
for char in line:
if char in " ?.!/;:":
line.replace(char,'')How do I do this properly?
See Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string? for the specific debugging question about what is wrong with this approach. Answers here mainly focus on how to solve the problem.
Solution
Strings in Python are immutable (can't be changed). Because of this, the effect of
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.
Starting in Python 2.6 and newer Python 2.x versions *, you can instead use
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
or regular expression replacement with
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
When calling the
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
%%CODEBLOCK_6%%
Here
line.replace(...) is just to create a new string, rather than changing the old one. You need to rebind (assign) it to line in order to have that variable take the new value, with those characters removed.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.
Starting in Python 2.6 and newer Python 2.x versions *, you can instead use
str.translate, (see Python 3 answer below):line = line.translate(None, '!@#
or regular expression replacement with re.sub
import re
line = re.sub('[!@#$]', '', line)
The characters enclosed in brackets constitute a character class. Any characters in line which are in that class are replaced with the second parameter to sub: an empty string.
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for str.translate.
When calling the translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.
So to do the above dance with a Unicode string you would call something like
translation_table = dict.fromkeys(map(ord, '!@#
Here dict.fromkeys and map are used to succinctly generate a dictionary containing
{ord('!'): None, ord('@'): None, ...}
Even simpler, as another answer puts it, create the translation table in place:
unicode_line = unicode_line.translate({ord(c): None for c in '!@#
Or, as brought up by Joseph Lee, create the same translation table with str.maketrans:
unicode_line = unicode_line.translate(str.maketrans('', '', '!@#
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of None:
import string
line = line.translate(string.maketrans('', ''), '!@#
Here string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)
or regular expression replacement with re.sub
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in line which are in that class are replaced with the second parameter to sub: an empty string.
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for str.translate.
When calling the translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here dict.fromkeys and map are used to succinctly generate a dictionary containing
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with str.maketrans:
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of None:
%%CODEBLOCK_6%%
Here string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.), None)
unicode_line = unicode_line.translate(translation_table)
Here dict.fromkeys and map are used to succinctly generate a dictionary containing
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with str.maketrans:
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of None:
%%CODEBLOCK_6%%
Here string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)
or regular expression replacement with re.sub
%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in line which are in that class are replaced with the second parameter to sub: an empty string.
Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for str.translate.
When calling the translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.
So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here dict.fromkeys and map are used to succinctly generate a dictionary containing
%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with str.maketrans:
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of None:
%%CODEBLOCK_6%%
Here string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.})
Or, as brought up by Joseph Lee, create the same translation table with str.maketrans:
%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of None:
%%CODEBLOCK_6%%
Here string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.), None)
unicode_line = unicode_line.translate(translation_table)Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.))* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.), None)
unicode_line = unicode_line.translate(translation_table)Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.})Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.), None)
unicode_line = unicode_line.translate(translation_table)Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.), None)
unicode_line = unicode_line.translate(translation_table)Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.})Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.), None)
unicode_line = unicode_line.translate(translation_table)Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.))* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.), None)
unicode_line = unicode_line.translate(translation_table)Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.})Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.), None)
unicode_line = unicode_line.translate(translation_table)Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.)or regular expression replacement with
re.sub%%CODEBLOCK_1%%
The characters enclosed in brackets constitute a character class. Any characters in
line which are in that class are replaced with the second parameter to sub: an empty string.Python 3 answer
In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for
str.translate.When calling the
translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.So to do the above dance with a Unicode string you would call something like
%%CODEBLOCK_2%%
Here
dict.fromkeys and map are used to succinctly generate a dictionary containing%%CODEBLOCK_3%%
Even simpler, as another answer puts it, create the translation table in place:
%%CODEBLOCK_4%%
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans:%%CODEBLOCK_5%%
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None:%%CODEBLOCK_6%%
Here
string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.Code Snippets
line = line.translate(None, '!@#$')import re
line = re.sub('[!@#$]', '', line)translation_table = dict.fromkeys(map(ord, '!@#$'), None)
unicode_line = unicode_line.translate(translation_table){ord('!'): None, ord('@'): None, ...}unicode_line = unicode_line.translate({ord(c): None for c in '!@#$'})Context
Stack Overflow Q#3939361, score: 787
Revisions (0)
No revisions yet.