简体   繁体   English

Python strip() 多个字符?

[英]Python strip() multiple characters?

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

Because that's not what strip() does.因为这不是strip()所做的。 It removes leading and trailing characters that are present in the argument, but not those characters in the middle of the string.它删除参数中存在的前导和尾随字符,但不会删除字符串中间的那些字符。

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)

I did a time test here, using each method 100000 times in a loop.我在这里做了一个时间测试,每个方法循环使用 100000 次。 The results surprised me.结果令我吃惊。 (The results still surprise me after editing them in response to valid criticism in the comments.) (针对评论中的有效批评对其进行编辑后,结果仍然让我感到惊讶。)

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.translate is not the most readable;但是,如果速度不是主要问题,我仍然认为string.translate不是最易读的; the other three are more obvious, though slower to varying degrees.其他三个比较明显,虽然有不同程度的慢。

string.translate with table=None works fine. string.translate with table=None 工作正常。

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

Because strip() only strips trailing and leading characters, based on what you provided.因为strip()仅根据您提供的内容去除尾随和前导字符。 I suggest:我建议:

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

strip only strips characters from the very front and back of the string. strip只从字符串的最前面和后面去除字符。

To delete a list of characters, you could use the string's translate method:要删除字符列表,您可以使用字符串的translate方法:

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM