python如何去除文本标点符号_python中如何去除标点符号

python中如何去除标点符号,写法,方法,字符,字母,都是

python中如何去除标点符号

易采站长站,站长之家为您整理了python中如何去除标点符号的相关内容。

Python去掉标点符号的方法如下:

方法一:

str.isalnum:

S.isalnum() -> bool

返回值:如果string至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False。

实例:>>> string = "Special $#! characters spaces 888323">>> ''.join(e for e in string if e.isalnum())'Specialcharactersspaces888323'

只能识别字母和数字,杀伤力大,会把中文、空格之类的也干掉

方法二:

string.punctuationimport re, strings ="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 categorys = 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 stringremove_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 unicodedataimport systbl = 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

例:import res ="string. With. Punctuation?"s = re.sub(r'[^\w\s]','',s)

测试:import re, string, timeits ="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 sprint"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

更多Python相关技术文章,请访问Python教程栏目进行学习!以上就是关于对python中如何去除标点符号的详细介绍。欢迎大家对python中如何去除标点符号内容提出宝贵意见

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值