特别篇:关于python解决字符串去除全部标点符号的方法

2017/07/12 Python

python解决字符串去除全部标点符号的方法

遇到去除字符串中所有标点符号的问题,想到的解决方法:

不用FOR循环,不用正则表达式

'''引入string模块'''
import string
'''使用标点符号常量'''
string.punctuation
text = "*/@》--【】--12()测试*()"

'''去除字符串中所有的字符,可增加自定义字符'''
def strclear(text,newsign=''):
    import string # 引入string模块
    signtext = string.punctuation + newsign # 引入英文符号常量,可附加自定义字符,默认为空
    signrepl = '@'*len(signtext) # 引入符号列表长度的替换字符
    signtable = str.maketrans(signtext,signrepl) # 生成替换字符表
    return text.translate(signtable).replace('@','') # 最后将替换字符替换为空即可

strclear(text,'》【】')
'12测试'

声明:转载请联系博主,并注明本博客及作者

Show Disqus Comments

Search

    欢迎关注我的微信公众号

    闷骚的程序员

    Table of Contents