Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

python - 从Python中的字符串中删除特定字符(Remove specific characters from a string in Python)

I'm trying to remove specific characters from a string using Python.

(我正在尝试使用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?

(如何正确执行此操作?)

  ask by Matt Phillips translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.fromkeysmap用于简洁地生成一个包含以下内容的字典)

{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的字符的字符串。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...