python去/替标点总结

首先是英文标点:import re, string, timeit

s = “string. With. Punctuation”exclude = set(string.punctuation)table = string.maketrans(“”,””)regex = re.compile(‘[%s]’ % re.escape(string.punctuation))

def test_set(s):return ”.join(ch for ch in s if ch not in exclude)

def test_re(s): # From Vinko’s solution, with fix.return regex.sub(”, s)

def test_trans(s):return s.translate(table, string.punctuation)

def test_repl(s): # From S.Lott’s solutionfor c in string.punctuation:s=s.replace(c,””)return s

print “sets :”,timeit.Timer(‘f(s)’, ‘from __main__ import s,test_set as f’).timeit(1000000)print “regex :”,timeit.Timer(‘f(s)’, ‘from __main__ import s,test_re as f’).timeit(1000000)print “translate :”,timeit.Timer(‘f(s)’, ‘from __main__ import s,test_trans as f’).timeit(1000000)print “replace :”,timeit.Timer(‘f(s)’, ‘from __main__ import s,test_repl as f’).timeit(1000000)速度对比:sets : 19.8566138744regex : 6.86155414581translate : 2.12455511093replace : 28.4436721802

然后是中文标点:

re.sub(u”[\uFF00-\uFFEF]+” ,’ ‘,str.decode(‘utf8′))

参考地址:

http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python

http://www.unicode.org/charts/PDF/UFF00.pdf

python去/替标点总结

相关文章:

你感兴趣的文章:

标签云: