from glob import glob
pattern = "D:\\report\\shakeall\\*.txt"
filelist = glob(pattern)
def countwords(fp):
with open(fp) as fh:
return len(fh.read().split())
print "There are" ,sum(map(countwords, filelist)), "words in the files. " "From directory",pattern
import os
uniquewords = set([])
for root, dirs, files in os.walk("D:\\report\\shakeall"):
for name in files:
[uniquewords.add(x) for x in open(os.path.join(root,name)).read().split()]
print "There are" ,len(uniquewords), "unique words in the files." "From directory", pattern
到目前为止我的代码是这样的。这计算了唯一单词的数量和总单词数D:\report\shakeall\*.txt
问题是,例如,这段代码识别code
code.
and code!
不同的词。因此,这不能回答确切数量的独特单词。
我想使用 Windows 文本编辑器从 42 个文本文件中删除特殊字符
或者制定一个例外规则来解决这个问题。
如果使用字母,我应该如何编写代码?
让它直接修改文本文件?或者例外,不计算特殊字符?
import re
string = open('a.txt').read()
new_str = re.sub('[^a-zA-Z0-9\n\.]', ' ', string)
open('b.txt', 'w').write(new_str)
它将把每个非字母数字字符更改为空格。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)