python 代码
功能如下:
将string里的所有的标点符号全部去除,只剩下英文字母,空格和数字
直接上代码:
# -*- coding: utf-8 -*-
import re
# make English text clean
def clean_text(text):
# keep English, digital and space
# 就是会把标点符号全部去除掉
comp = re.compile('[^A-Z^a-z^0-9^ ]')
return comp.sub('', text)
sentence='Hello! Dear world! I am happy today... Okay, fine, actually, that is not.'
output=clean_text(sentence)
print(output)
#Hello Dear world I am happy today Okay fine actually that is not
最后output的结果就是
Hello Dear world I am happy today Okay fine actually that is not
本文介绍了一个Python函数,通过正则表达式实现去除字符串中所有标点符号,只保留字母、数字和空格,以'HelloDearworldIamhappytoday...'为例进行演示。
5072

被折叠的 条评论
为什么被折叠?



