I want create a catalog of all logs, so I just want keep all punctuation and remove all other chars which include CJK and others.
我想创建所有日志的目录,所以我只想保留所有标点符号并删除所有其他包含CJK和其他字符的字符。
for example:
例如:
s = "aaa; sf = fa = bla http://wa"
expected output is
预期产量是
;==://
2 个解决方案
#1
16
You can use str.translate
:
你可以使用str.translate:
>>> from string import letters, digits, whitespace, punctuation
>>> s = "aaa; sf = fa = bla http://wa"
>>> s.translate(None, letters+digits+whitespace)
';==://'
or regex
:
或正则表达式:
>>> re.sub(r'[^{}]+'.format(punctuation),'',s)
';==://'
Timing comparisons:
时间比较:
>>> s = "aaa; sf = fa = bla http://wa"*1000
>>> %timeit s.translate(None,letters+digits+whitespace)
10000 loops, best of 3: 171 us per loop #winner
>>> r1 = re.compile(r'[^{}]+'.format(punctuation))
>>> r2 = re.compile(r'[\w\s]+')
>>> %timeit r1.sub('',s)
100 loops, best of 3: 2.64 ms per loop
>>> %timeit r2.sub('',s)
100 loops, best of 3: 3.31 ms per loop
#2
2
Using a regular expression:
使用正则表达式:
>>> re.sub(r'[\w\s]+', '', "aaa; sf = fa = bla http://wa")
';==://'
Compiling can buy some performance, even for such a simple pattern...
编译可以购买一些性能,即使对于这样一个简单的模式......
>>> %timeit re.sub(r'[\w\s]+', '', "aaa; sf = fa = bla http://wa")
100000 loops, best of 3: 6.78 us per loop
>>> e = re.compile(r'[\w\s]+')
>>> %timeit e.sub('', "aaa; sf = fa = bla http://wa")
100000 loops, best of 3: 4.91 us per loop
...but regular expression is no match for Ashwinis' solution using str.translate:
...但正则表达式与使用str.translate的Ashwinis解决方案不匹配:
>>> %timeit "aaa; sf = fa = bla http://wa".translate(None,letters+digits+whitespace)
1000000 loops, best of 3: 1.31 us per loop
#1
16
You can use str.translate
:
你可以使用str.translate:
>>> from string import letters, digits, whitespace, punctuation
>>> s = "aaa; sf = fa = bla http://wa"
>>> s.translate(None, letters+digits+whitespace)
';==://'
or regex
:
或正则表达式:
>>> re.sub(r'[^{}]+'.format(punctuation),'',s)
';==://'
Timing comparisons:
时间比较:
>>> s = "aaa; sf = fa = bla http://wa"*1000
>>> %timeit s.translate(None,letters+digits+whitespace)
10000 loops, best of 3: 171 us per loop #winner
>>> r1 = re.compile(r'[^{}]+'.format(punctuation))
>>> r2 = re.compile(r'[\w\s]+')
>>> %timeit r1.sub('',s)
100 loops, best of 3: 2.64 ms per loop
>>> %timeit r2.sub('',s)
100 loops, best of 3: 3.31 ms per loop
#2
2
Using a regular expression:
使用正则表达式:
>>> re.sub(r'[\w\s]+', '', "aaa; sf = fa = bla http://wa")
';==://'
Compiling can buy some performance, even for such a simple pattern...
编译可以购买一些性能,即使对于这样一个简单的模式......
>>> %timeit re.sub(r'[\w\s]+', '', "aaa; sf = fa = bla http://wa")
100000 loops, best of 3: 6.78 us per loop
>>> e = re.compile(r'[\w\s]+')
>>> %timeit e.sub('', "aaa; sf = fa = bla http://wa")
100000 loops, best of 3: 4.91 us per loop
...but regular expression is no match for Ashwinis' solution using str.translate:
...但正则表达式与使用str.translate的Ashwinis解决方案不匹配:
>>> %timeit "aaa; sf = fa = bla http://wa".translate(None,letters+digits+whitespace)
1000000 loops, best of 3: 1.31 us per loop