小编roc*_*and的帖子

计算python中的唯一单词

直接,我的代码到目前为止是这样的:

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
Run Code Online (Sandbox Code Playgroud)

我想添加一个代码来计算模式中的唯一单词(此路径中有42个txt文件),但我不知道如何.有谁能够帮我?

python word-count

4
推荐指数
1
解决办法
1万
查看次数

如何使用Python从txt文件中删除特殊字符

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
Run Code Online (Sandbox Code Playgroud)

到目前为止我的代码是这样的.这会计算来自的唯一单词和总单词的数量D:\report\shakeall\*.txt

问题是,例如,此代码识别code code.code!不同的单词.因此,这不能解决确切数量的独特单词.

我想使用Windows文本编辑器从42个文本文件中删除特殊字符

或者制定解决此问题的例外规则.

如果使用后者,我的代码怎么样?

让它直接修改文本文件?或者做一个不计算特殊字符的异常?

python

4
推荐指数
1
解决办法
2万
查看次数

我不理解Python中的'from'

可能重复:
'import module'或'from module
from … importimport'vsimport .

from glob import glob
from os.path import isfile
def countwords(fp):
   with open(fp) as fh:
       return len(fh.read().split())

print "There are" ,sum(map(countwords, filter(isfile, glob("*.txt") ) ) ), "words in the files."
Run Code Online (Sandbox Code Playgroud)

在第一行,为什么不只是简单地导入glob库?

有没有理由在"import glob"前面使用"from glob"?

python glob

2
推荐指数
1
解决办法
3514
查看次数

标签 统计

python ×3

glob ×1

word-count ×1