Strings in Python are immutable (can't be changed).
(Python中的字符串是不可变的 (无法更改)。)
Because of this, the effect of line.replace(...)
is just to create a new string, rather than changing the old one. (因此, line.replace(...)
作用只是创建一个新字符串,而不是更改旧字符串。)
You need to rebind (assign) it to line
in order to have that variable take the new value, with those characters removed. (您需要将其重新绑定 (分配)到line
中,以使该变量采用新值,并删除这些字符。)
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. (这也可能会使经验丰富的pythonator感到有些困惑,他们将看到双重嵌套的结构,并暂时认为会发生一些更复杂的事情。)
Starting in Python 2.6 and newer Python 2.x versions *, you can instead use str.translate
, (but read on for Python 3 differences):
(从Python 2.6和更高版本的Python 2.x版本*开始,您可以改用str.translate
(但请继续阅读Python 3的不同之处):)
line = line.translate(None, '!@#$')
or regular expression replacement with re.sub
(或用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. (该类中所有line
中的字符都将替换为sub
的第二个参数:空字符串。)
In Python 3, strings are Unicode.
(在Python 3中,字符串是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
. (kevpie在对其中一个答案的评论中提到了这一点,并在str.translate
的文档中str.translate
进行了说明 。)
When calling the translate
method of a Unicode string, you cannot pass the second parameter that we used above.
(当调用Unicode字符串的translate
方法时,您不能传递上面使用的第二个参数。)
You also can't pass None
as the first parameter, or even a translation table from string.maketrans
. (您也不能传递None
作为第一个参数,甚至不能传递string.maketrans
的转换表。)
Instead, you pass a dictionary as the only parameter. (相反,您将字典作为唯一参数传递。)
This dictionary maps the ordinal values of characters (ie 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. (该词典将字符的序数值 (即对它们调用ord
的结果)映射到应替换它们的字符的序数值,或者(对我们有用的是- None
表示应删除它们。)
So to do the above dance with a Unicode string you would call something like
(因此,使用Unicode字符串进行上述舞蹈时,您会调用类似)
translation_table = dict.fromkeys(map(ord, '!@#$'), None)
unicode_line = unicode_line.translate(translation_table)
Here dict.fromkeys
and map
are used to succinctly generate a dictionary containing
(这里dict.fromkeys
和map
用于简洁地生成一个包含以下内容的字典)
{ord('!'): None, ord('@'): None, ...}
Even simpler, as another answer puts it , create the dictionary in place:
(就像另一个答案所说的那样 ,甚至更简单,在适当的位置创建字典:)
unicode_line = unicode_line.translate({ord(c): None for c in '!@#$'})
* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of None
:
(*为了与早期的Python兼容,您可以创建一个“空”转换表来代替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.
(在这里, string.maketrans
用于创建转换表 ,该表只是一个包含序号为0到255的字符的字符串。)