string.punctuation
统计一个文件或一个字符串中所有单词出现的次数。由于句子中存在标点符号,直接对字符串切割的话会把单词和标点切割在一起,比如:
s="We met at the wrong time, but separated at the right time. The most urgen
t is to take the most beautiful scenery!!! the deepest wound was the most real e
motions."
for i in s:
if i in string.punctuation: #如果字符是标点符号的话就将其替换为空格
s = s.replace(i," ")
print(s)
输出:'We met at the wrong time but separated at the right time The most urgent is to take the most beautiful scenery the deepest wound was the most real emotions '
s.split()#按空白切割
输出:['We', 'met', 'at', 'the', 'wrong', 'time', 'but', 'separated', 'at', 'the', 'ri
ght', 'time', 'The', 'most', 'urgent', 'is', 'to', 'take', 'the', 'most', 'beaut
iful', 'scenery', 'the', 'deepest', 'wound', 'was', 'the', 'most', 'real', 'emot
ions']