python两个方法删除文本中的标点符号

1.正则表达式(可以删除中英文字符)

import re
a=re.sub(r'[\W]','',s)
. 替换任意1个字符(除了\n)
[ ] 替换[ ]中列举的字符
\d 替换数字,即0-9
\D 替换非数字
\s 替换空格,tab键
\S 替换非空白
\w 替换非特殊字符,即a-z、A-Z、0-9、_、汉字
\W 替换特殊字符,即非字母、非数字、非汉字、非_

2.使用string模块的punctuation函数

1)删除英文字符

import string
for i in string.punctuation:
    s=s.replace(i,'')//replace(,)是对字符串进行修改,remove()是对列表进行修改
print(s)

2)删除中文字符

!pop install zhon//进行下载

from zhon.hanzi import punctuation
for i in punctuation:
    s=s.replace(i,'')
print(s)

你可能感兴趣的:(总结回顾,python,list)