赵走x博客
网站访问量:151539
首页
书籍
软件
工具
古诗词
搜索
登录
24、nonlocal
23、Python functools.wraps 深入理解
21、Image.rotate旋转90度图片被截取了
20、python的__get__方法看这一篇就足够了
19、结巴分词(自然语言处理之中文分词器)
18、virtualenv
17、Python string 去掉标点符号 最佳实践
16、python 字符串相似度
15、StringIO和BytesIO
14、基于Python __dict__与dir()的区别详解
13、 set()去重的底层原理
12、顺序表的原理与python中的list类型
11、psutil实现系统监控
10、NSQ
9、utf-8的中文是一个汉字占三个字节长度吗?
8、supervisor+gunicorn部署python web项目
7、socket编程
6、async
5、Python的共享经济
4、Python 内存分配时的小秘密
3、Python中的“特权种族”是什么?
2、装饰器
1、序列化与反序列化
17、Python string 去掉标点符号 最佳实践
资源编号:76593
Python
Python 查缺补漏
热度:100
参考:https://blog.csdn.net/chihwei_hsu/article/details/81604272
# 方法一: str.isalnum: >S.isalnum() -> bool Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise. ```python >>> string = "Special $#! characters spaces 888323" >>> ''.join(e for e in string if e.isalnum()) 'Specialcharactersspaces888323' ``` 特点: 只能识别字母和数字,杀伤力大,会把中文、空格之类的也干掉 # 方法二: string.punctuation ```python import re, string s ="string. With. Punctuation?" # Sample string # 写法一: out = s.translate(string.maketrans("",""), string.punctuation) # 写法二: out = s.translate(None, string.punctuation) # 写法三: exclude = set(string.punctuation) out = ''.join(ch for ch in s if ch not in exclude) # 写法四: >>> for c in string.punctuation: s = s.replace(c,"") >>> s 'string With Punctuation' # 写法五: out = re.sub('[%s]' % re.escape(string.punctuation), '', s) ## re.escape:对字符串中所有可能被解释为正则运算符的字符进行转义 # 写法六: # string.punctuation 只包括 ascii 格式; 想要一个包含更广(但是更慢)的方法是使用: unicodedata module : from unicodedata import category s = u'String — with - «Punctuation »...' out = re.sub('[%s]' % re.escape(string.punctuation), '', s) print 'Stripped', out # 输出:u'Stripped String \u2014 with \xabPunctuation \xbb' out = ''.join(ch for ch in s if category(ch)[0] != 'P') print 'Stripped', out # 输出:u'Stripped String with Punctuation ' # For Python 3 str or Python 2 unicode values, str.translate() only takes a dictionary; codepoints (integers) are looked up in that mapping and anything mapped to None is removed. # To remove (some?) punctuation then, use: import string remove_punct_map = dict.fromkeys(map(ord, string.punctuation)) s.translate(remove_punct_map) # Your method doesn't work in Python 3, as the translate method doesn't accept the second argument any more. import unicodedata import sys tbl = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P')) def remove_punctuation(text): return text.translate(tbl) ``` # 方法三: re ```python import re s ="string. With. Punctuation?" s = re.sub(r'[^\w\s]','',s) ``` 测试: ```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): return regex.sub('', s) def test_trans(s): return s.translate(table, string.punctuation) def test_repl(s): for 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) ``` out_put: ``` # sets : 19.8566138744 # regex : 6.86155414581 # translate : 2.12455511093 # replace : 28.4436721802 ```