从Python中的字符串中删除特定字符
我正在尝试使用 Python 从字符串中删除特定字符。这是我现在正在使用的代码。不幸的是,它似乎对字符串没有任何作用。
for char in line:
if char in " ?.!/;:":
line.replace(char,'')
我该如何正确地做到这一点?
请参阅为什么调用字符串方法(例如 .replace 或 .strip)不会修改(变异)字符串?有关此方法有什么问题的具体调试问题。这里的回答主要集中在如何解决问题。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
Python 中的字符串是不可变的(无法更改)。因此,line.replace(...) 的作用只是创建一个新字符串,而不是更改旧字符串。您需要将其重新绑定(分配)到
line
,以便使该变量采用新值,并删除这些字符。而且,相对来说,你这样做的方式会有点慢。对于经验丰富的 Python 开发者来说,这也可能会有点令人困惑,他们会看到双重嵌套结构,并暂时认为正在发生更复杂的事情。
从 Python 2.6 和更新的 Python 2.x 版本开始*,您可以改为使用
str.translate
,(请参阅下面的 Python 3 答案):或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
))string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
})string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
})string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
))string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
})string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
)string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。或将正则表达式替换为
re.sub
括号内的字符构成一个字符类。该类中的
line
中的任何字符都将替换为sub
的第二个参数:空字符串。Python 3 答案
在Python 3 中,字符串是Unicode。你必须翻译得有点不同。 kevpie 在评论中提到了这一点答案,并在
str.translate 文档中注明
。当调用 Unicode 字符串的
translate
方法时,不能传递我们上面使用的第二个参数。您也不能将None
作为第一个参数传递。相反,您传递一个翻译表(通常是字典)作为唯一的参数。该表映射字符的序数值(即调用ord
)到应替换它们的字符的序数值,或者(对我们有用)None
表示应删除它们。因此,要使用 Unicode 字符串执行上述舞蹈,您可以在此处调用类似
<代码>dict.fromkeys和
map< /code>
用于简洁地生成一个包含
更简单的字典,如 另一个答案提出了,创建翻译表:
或者,如 Joseph Lee,使用
str.maketrans
:* 为了与早期的 Python 兼容,您可以创建一个“空”转换表来代替
None
:此处
string.maketrans
用于创建一个翻译表,其中只是一个包含序数值为 0 到 255 的字符的字符串。Strings in Python are immutable (can't be changed). Because of this, the effect of
line.replace(...)
is just to create a new string, rather than changing the old one. You need to rebind (assign) it toline
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):or regular expression replacement with
re.sub
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
:Here
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
:Here
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
:Here
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
:Here
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
:Here
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
:Here
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
:Here
), None) unicode_line = unicode_line.translate(translation_table)string.maketrans
is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
: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
The characters enclosed in brackets constitute a character class. Any characters in
line
which are in that class are replaced with the second parameter tosub
: 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 passNone
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 callingord
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
Here
dict.fromkeys
andmap
are used to succinctly generate a dictionary containingEven simpler, as another answer puts it, create the translation table in place:
Or, as brought up by Joseph Lee, create the same translation table with
str.maketrans
:* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of
None
:Here
string.maketrans
is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.我是否错过了这里的重点,或者只是以下内容:
将其放入循环中:
Am I missing the point here, or is it just the following:
Put it in a loop:
使用
re.sub
正则表达式从 Python 3.5 开始,使用正则表达式
re.sub
进行替换变得可用:示例
解释
正则表达式 (regex),
|
是逻辑 OR 和\
转义可能是实际正则表达式命令的空格和特殊字符。而sub
代表替换,在本例中为空字符串''
。With
re.sub
regular expressionSince Python 3.5, substitution using regular expressions
re.sub
became available:Example
Explanation
In regular expressions (regex),
|
is a logical OR and\
escapes spaces and special characters that might be actual regex commands. Whereassub
stands for substitution, in this case with the empty string''
.提问者几乎已经明白了。就像 Python 中的大多数东西一样,答案比你想象的要简单。
您不必执行嵌套的 if/for 循环,但您需要单独检查每个字符。
The asker almost had it. Like most things in Python, the answer is simpler than you think.
You don't have to do the nested if/for loop thing, but you DO need to check each character individually.
对于字符串中仅允许某些字符的反向要求,您可以使用带有补集运算符
[^ABCabc]
的正则表达式。例如,要删除除 ascii 字母、数字和连字符之外的所有内容:来自 python 正则表达式文档 :
For the inverse requirement of only allowing certain characters in a string, you can use regular expressions with a set complement operator
[^ABCabc]
. For example, to remove everything except ascii letters, digits, and the hyphen:From the python regular expression documentation:
Python 中的字符串是不可变的。
replace
方法在替换后返回一个新字符串。尝试:这与您的原始代码相同,只是在循环内添加了对
line
的赋值。请注意,字符串
replace()
方法会替换字符串中出现的所有个字符,因此您可以使用replace()
做得更好> 对于要删除的每个字符,而不是循环遍历字符串中的每个字符。Strings are immutable in Python. The
replace
method returns a new string after the replacement. Try:This is identical to your original code, with the addition of an assignment to
line
inside the loop.Note that the string
replace()
method replaces all of the occurrences of the character in the string, so you can do better by usingreplace()
for each character you want to remove, instead of looping over each character in your string.试试这个:
这个方法在 Python 3 中运行良好
Try this one:
This method works well in Python 3
令我惊讶的是,还没有人推荐使用内置的 filter 功能。
假设我们要过滤掉所有不是数字的内容。使用过滤器内置方法“...相当于生成器表达式(item for item in iterable if function(item))”[Python 3 内置函数:Filter]
在 Python 3 中,此返回
To get a print string,
I am not certain how filter 在效率方面排名,但在进行列表推导等操作时了解如何使用是一件好事。
更新
从逻辑上讲,由于过滤器有效,您也可以使用列表理解,并且根据我的阅读,它应该更有效,因为 lambda 是编程函数世界的华尔街对冲基金经理。另一个优点是它是一种不需要任何进口的单线产品。例如,使用上面定义的相同字符串“s”,
就是这样。返回将是原始字符串中所有数字字符的字符串。
如果您有可接受/不可接受字符的特定列表,则只需调整列表理解的“if”部分。
或者,
I was surprised that no one had yet recommended using the builtin filter function.
Say we want to filter out everything that isn't a number. Using the filter builtin method "...is equivalent to the generator expression (item for item in iterable if function(item))" [Python 3 Builtins: Filter]
In Python 3 this returns
To get a printed string,
I am not sure how filter ranks in terms of efficiency but it is a good thing to know how to use when doing list comprehensions and such.
UPDATE
Logically, since filter works you could also use list comprehension and from what I have read it is supposed to be more efficient because lambdas are the wall street hedge fund managers of the programming function world. Another plus is that it is a one-liner that doesnt require any imports. For example, using the same string 's' defined above,
That's it. The return will be a string of all the characters that are digits in the original string.
If you have a specific list of acceptable/unacceptable characters you need only adjust the 'if' part of the list comprehension.
or alternatively,
使用
filter
,你只需要一行这会将字符串视为可迭代对象,并检查每个字符是否
lambda
返回True
:Using
filter
, you'd just need one lineThis treats the string as an iterable and checks every character if the
lambda
returnsTrue
:以下是完成此任务的一些可能方法:
PS:示例使用元音代替“?.!/;:”...是的,“murcielago”是西班牙语单词,表示“蝙蝠”...有趣的单词,因为它包含所有元音:)
PS2:如果您对性能感兴趣,您可以使用简单的代码来衡量这些尝试,例如:
在我的框中,您会得到:
所以看来尝试4对于该特定输入来说是最快的。
Here's some possible ways to achieve this task:
PS: Instead using " ?.!/;:" the examples use the vowels... and yeah, "murcielago" is the Spanish word to say bat... funny word as it contains all the vowels :)
PS2: If you're interested on performance you could measure these attempts with a simple code like:
In my box you'd get:
So it seems attempt4 is the fastest one for this particular input.
这是我的 Python 2/3 兼容版本。由于翻译 api 已更改。
Here's my Python 2/3 compatible version. Since the translate api has changed.
您还可以使用函数来使用列表替换不同类型的正则表达式或其他模式。这样,您就可以混合正则表达式、字符类和真正基本的文本模式。当您需要替换大量元素(例如 HTML 元素)时,它非常有用。
*注意:适用于 Python 3.x
在函数 string_cleanup 中,它将字符串 x 和不需要的列表作为参数。对于该元素或模式列表中的每个项目,如果需要替代品,就会完成。
输出:
You can also use a function in order to substitute different kind of regular expression or other pattern with the use of a list. With that, you can mixed regular expression, character class, and really basic text pattern. It's really useful when you need to substitute a lot of elements like HTML ones.
*NB: works with Python 3.x
In the function string_cleanup, it takes your string x and your list notwanted as arguments. For each item in that list of elements or pattern, if a substitute is needed it will be done.
The output:
我使用的方法可能不会那么有效,但它非常简单。我可以使用切片和格式化一次性删除不同位置的多个字符。
这是一个例子:
这将导致“removed”包含“this”一词。
格式化对于在打印字符串中打印变量非常有帮助。它可以使用 % 后跟变量的数据类型来插入任何数据类型;所有数据类型都可以使用%s,浮点数(也称为小数)和整数可以使用%d。
切片可用于对字符串进行复杂的控制。当我输入 words[:3] 时,它允许我选择字符串中从开头(冒号在数字之前,这意味着“从头到”)到第4个字符(包含第4个字符)。 3 等于第四个位置的原因是因为 Python 从 0 开始。然后,当我输入 word[-1:] 时,它表示倒数第二个字符(冒号在数字后面) )。输入 -1 将使 Python 从最后一个字符而不是第一个字符开始计数。同样,Python 将从 0 开始。因此,word[-1:] 基本上表示“从倒数第二个字符到字符串末尾”。
因此,通过切断我想要删除的字符之前的字符和之后的字符并将它们夹在一起,我可以删除不需要的字符。 把它想象成一根香肠。中间很脏,所以我想把它扔掉。我只是剪掉我想要的两端,然后将它们放在一起,中间没有不需要的部分。
如果我想删除多个连续的字符,我只需在 [] (切片部分)中移动数字即可。或者,如果我想从不同位置删除多个字符,我可以简单地一次将多个切片夹在一起。
示例:
删除等于“酷”。
删除等于“macs”。
在本例中,[3:5] 表示位置 3 处的字符到位置 5 处的字符(不包括最终位置处的字符)。
请记住,Python 从 0 开始计数,因此您也需要这样做。
My method I'd use probably wouldn't work as efficiently, but it is massively simple. I can remove multiple characters at different positions all at once, using slicing and formatting.
Here's an example:
This will result in 'removed' holding the word 'this'.
Formatting can be very helpful for printing variables midway through a print string. It can insert any data type using a % followed by the variable's data type; all data types can use %s, and floats (aka decimals) and integers can use %d.
Slicing can be used for intricate control over strings. When I put words[:3], it allows me to select all the characters in the string from the beginning (the colon is before the number, this will mean 'from the beginning to') to the 4th character (it includes the 4th character). The reason 3 equals till the 4th position is because Python starts at 0. Then, when I put word[-1:], it means the 2nd last character to the end (the colon is behind the number). Putting -1 will make Python count from the last character, rather than the first. Again, Python will start at 0. So, word[-1:] basically means 'from the second last character to the end of the string.
So, by cutting off the characters before the character I want to remove and the characters after and sandwiching them together, I can remove the unwanted character. Think of it like a sausage. In the middle it's dirty, so I want to get rid of it. I simply cut off the two ends I want then put them together without the unwanted part in the middle.
If I want to remove multiple consecutive characters, I simply shift the numbers around in the [] (slicing part). Or if I want to remove multiple characters from different positions, I can simply sandwich together multiple slices at once.
Examples:
removed equals 'cool'.
removed equals 'macs'.
In this case, [3:5] means character at position 3 through character at position 5 (excluding the character at the final position).
Remember, Python starts counting at 0, so you will need to as well.
在 Python 3.5 中
例如,
,从字符串中删除所有数字
In Python 3.5
e.g.,
To remove all the number from the string
以下花絮已在此线程中进行了解释。我只是把它放在一起作为答案。
Tidbits of the following have been explained already in this thread. I am just putting it together as an answer.
这个怎么样:
How about this:
下面一个..不使用正则表达式概念..
Below one.. with out using regular expression concept..
递归分割:
s=字符串; chars=要删除的字符
示例:
Recursive split:
s=string ; chars=chars to remove
example:
您可以使用 re 模块的正则表达式替换。使用 ^ 表达式可以让您从字符串中准确选择您想要的内容。
输出将是“Thisisabsurd”。仅显示 ^ 符号之后指定的内容。
You could use the re module's regular expression replacement. Using the ^ expression allows you to pick exactly what you want from your string.
Output to this would be "Thisisabsurd". Only things specified after the ^ symbol will appear.
# 对于目录中的每个文件,重命名文件名
# for each file on a directory, rename filename
即使下面的方法也可以
输出:
abcde
Even the below approach works
output:
abcde
字符串方法
replace
不会修改原始字符串。它保留原始版本并返回修改后的副本。您想要的是这样的:
line = line.replace(char,'')
但是,每次删除字符时都创建一个新字符串,效率非常低。我推荐以下内容:
The string method
replace
does not modify the original string. It leaves the original alone and returns a modified copy.What you want is something like:
line = line.replace(char,'')
However, creating a new string each and every time that a character is removed is very inefficient. I recommend the following instead:
如果您希望字符串仅使用 ASCII 代码允许的字符,您可以使用这段代码:
它将删除 a....z 之外的所有字符,甚至是大写字母。
If you want your string to be just allowed characters by using ASCII codes, you can use this piece of code:
It will remove all the characters beyond a....z even upper cases.