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
import re
import string
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()]
wordlist = list(uniquewords)
이 코드는 고유 한 총 단어 수를 계산합니다. 그러나 문제는 len (uniquewords)을 쓰면, 예를 들어 '쉐이크' '쉐이크'를 인식하기 때문에 불합리한 수를 보여줍니다. '흔들어 라!'하고 '흔들어 라?' 다른 고유 한 단어로. 목록을 만들고 수정하여 모든 유니크 워드에서 구두점을 제거하려고 시도했습니다. 모든 것이 실패했습니다. 아무도 나를 도울 수 있습니까? \w+
패턴파이썬을 사용하여 목록 항목에서 구두점 제거하기
[SO에 코드를 형식하는 방법] (http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) – Levon
' 파일에서 단어 ""디렉토리에서 "단순히 파일의 단어가 아닙니다." "디렉토리에서? – Levon
http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python –