import re
def strip(text, chars=None):
"""去除首尾的字符
:type text: string
:type chars: string
:rtype: string
"""
if chars is None:
reg = re.compile('^ *| *$')
else:
reg = re.compile(r'^[' + chars + ']*|[' + chars + ']*$')
return reg.sub('', text) #把text里符合reg格式的字符串替换成'',也即去掉该字符串
#r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
text = "zhong xin gssang! haha...li{li},ha.yooook'''sss-s(good)&ls\"d\" \lixingdd_ddd#\ll#1+2-3/4*8=5@@7$1%2:3;4<1>2?3^7`0[11]0~9dd\n00\n\n"
print(re.split('!| |\.|{|}|,|-|!|\(|\)|&|\"|_|\\\\|#|\+|\*|/|=|@|\$|%|:|;|<|>|\?|\^|\[|\]|\`|\~|\\n', text))
print(re.sub('!| |\.|{|}|,|-|!|\(|\)|&|\"|_|\\\\|#|\+|\*|/|=|@|\$|%|:|;|<|>|\?|\^|\[|\]|\`|\~|\\n', ' ', text))
最后输出
['zhong', 'xin', 'gssang', '', 'haha', '', '', 'li', 'li', '', 'ha', "yooook'''sss", 's', 'good', '', 'ls', 'd', '', '', 'lixingdd', 'ddd', '', 'll', '1', '2', '3', '4', '8', '5', '', '7', '1', '2', '3', '4', '1', '2', '3', '7', '0', '11', '0', '9dd', '00', '', '']
zhong xin gssang haha li li ha yooook'''sss s good ls d lixingdd ddd ll 1 2 3 4 8 5 7 1 2 3 4 1 2 3 7 0 11 0 9dd 00