从Python中的字符串中删除特定字符 [英] Remove specific characters from a string in Python

查看:1358
本文介绍了从Python中的字符串中删除特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python从字符串中删除特定字符.这是我现在正在使用的代码.不幸的是,它似乎对字符串没有任何作用.

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,'')

如何正确执行此操作?

推荐答案

Python中的字符串是不可变的(无法更改).因此,line.replace(...)的作用只是创建一个新字符串,而不是更改旧字符串.您需要重新绑定(将其分配给line),以使该变量采用新值,并删除这些字符.

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 to line in order to have that variable take the new value, with those characters removed.

此外,相对而言,您的操作方式会比较缓慢.这也可能会使经验丰富的pythonator感到有些困惑,他们会看到双重嵌套的结构,并一会儿认为会发生一些更复杂的事情.

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.

从Python 2.6和更高版本的Python 2.x版本开始*,您可以改为使用

Starting in Python 2.6 and newer Python 2.x versions *, you can instead use str.translate, (but read on for Python 3 differences):

line = line.translate(None, '!@#$')

或用 re.sub 替换的正则表达式>

or regular expression replacement with re.sub

import re
line = re.sub('[!@#$]', '', line)

括在方括号中的字符构成一个字符类. line中该类中的所有字符都将替换为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 to sub: an empty string.

在Python 3中,字符串是Unicode.您将不得不进行一些不同的翻译. kevpie在评论中提到了这一点答案,并在str.translate 文档中进行了记录>.

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.

当调用Unicode字符串的translate方法时,不能传递上面使用的第二个参数.您也不能将None作为第一个参数传递.相反,您将翻译表(通常是字典)作为唯一参数传递.该表映射了字符的原始值(即调用

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.

因此,要使用Unicode字符串进行上述舞蹈,您会称呼类似

So to do the above dance with a Unicode string you would call something like

translation_table = dict.fromkeys(map(ord, '!@#$'), None)
unicode_line = unicode_line.translate(translation_table)

此处 dict.fromkeys

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 '!@#$'})

或使用 str.maketrans 创建相同的翻译表:

Or create the same translation table with str.maketrans:

unicode_line = unicode_line.translate(str.maketrans('', '', '!@#$'))


*为了与早期的Python兼容,您可以创建一个空"转换表来代替None:

import string
line = line.translate(string.maketrans('', ''), '!@#$')

此处 string.maketrans 用于创建翻译表,它只是一个包含序号为0到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.

这篇关于从Python中的字符串中删除特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆