Python strip() 多个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3900054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:16:37  来源:igfitidea点击:

Python strip() multiple characters?

python

提问by AP257

I want to remove any brackets from a string. Why doesn't this work properly?

我想从字符串中删除任何括号。为什么这不能正常工作?

>>> name = "Barack (of Washington)"
>>> name = name.strip("(){}<>")
>>> print name
Barack (of Washington

采纳答案by JasonFruit

I did a time test here, using each method 100000 times in a loop. The results surprised me. (The results still surprise me after editing them in response to valid criticism in the comments.)

我在这里做了一个时间测试,每个方法循环使用 100000 次。结果令我吃惊。(针对评论中的有效批评对其进行编辑后,结果仍然让我感到惊讶。)

Here's the script:

这是脚本:

import timeit

bad_chars = '(){}<>'

setup = """import re
import string
s = 'Barack (of Washington)'
bad_chars = '(){}<>'
rgx = re.compile('[%s]' % bad_chars)"""

timer = timeit.Timer('o = "".join(c for c in s if c not in bad_chars)', setup=setup)
print "List comprehension: ",  timer.timeit(100000)


timer = timeit.Timer("o= rgx.sub('', s)", setup=setup)
print "Regular expression: ", timer.timeit(100000)

timer = timeit.Timer('for c in bad_chars: s = s.replace(c, "")', setup=setup)
print "Replace in loop: ", timer.timeit(100000)

timer = timeit.Timer('s.translate(string.maketrans("", "", ), bad_chars)', setup=setup)
print "string.translate: ", timer.timeit(100000)

Here are the results:

结果如下:

List comprehension:  0.631745100021
Regular expression:  0.155561923981
Replace in loop:  0.235936164856
string.translate:  0.0965719223022

Results on other runs follow a similar pattern. If speed is not the primary concern, however, I still think string.translateis not the most readable; the other three are more obvious, though slower to varying degrees.

其他运行的结果遵循类似的模式。但是,如果速度不是主要考虑因素,我仍然认为string.translate它不是最易读的;其他三个比较明显,虽然有不同程度的慢。

回答by unutbu

striponly strips characters from the very front and back of the string.

strip只从字符串的最前面和后面去除字符。

To delete a list of characters, you could use the string's translatemethod:

要删除字符列表,您可以使用字符串的translate方法:

import string
name = "Barack (of Washington)"
table = string.maketrans( '', '', )
print name.translate(table,"(){}<>")
# Barack of Washington

回答by bobince

Because that's not what strip()does. It removes leading and trailing characters that are present in the argument, but not those characters in the middle of the string.

因为那不是什么strip()。它删除参数中存在的前导和尾随字符,但不会删除字符串中间的那些字符。

You could do:

你可以这样做:

name= name.replace('(', '').replace(')', '').replace ...

or:

或者:

name= ''.join(c for c in name if c not in '(){}<>')

or maybe use a regex:

或者使用正则表达式:

import re
name= re.sub('[(){}<>]', '', name)

回答by Ruel

Because strip()only strips trailing and leading characters, based on what you provided. I suggest:

因为strip()仅根据您提供的内容去除尾随和前导字符。我建议:

>>> import re
>>> name = "Barack (of Washington)"
>>> name = re.sub('[\(\)\{\}<>]', '', name)
>>> print(name)
Barack of Washington

回答by cherish

string.translatewith table=None works fine.

string.translatewith table=None 工作正常。

>>> name = "Barack (of Washington)"
>>> name = name.translate(None, "(){}<>")
>>> print name
Barack of Washington

回答by Siva Kumar

For example string s="(U+007c)"

例如字符串 s="(U+007c)"

To remove only the parentheses from s, try the below one:

要仅删除 s 中的括号,请尝试以下方法:

import re
a=re.sub("\(","",s)
b=re.sub("\)","",a)
print(b)