当前位置: 首页 > 工具软件 > Walden > 使用案例 >

文档读取 Walden

张和颂
2023-12-01

文档读取并计算单词频率

import collections          # 收集数据
f=open("D:\python\Walden.txt").read()    # 打开需要读取的文档
f=f.replace(',','').replace('.','').replace('"','').replace('!','').replace('?','').replace(':','')  # 把标点符号替换成空格
f=f.split()                  # 将文档单词化
r=collections.Counter(f)     # 数出每个单词出现频率
print(r)                     # 输出
 类似资料: