我在Windows上使用Python3。如何包含多个文本文件,以便可以使用多个文件运行代码?
article_one = re.findall('\w+', open('D.txt',).read().lower())
wordbank = {}
for word in article_one:
word = word.lower().strip(string.punctuation)
if word not in wordbank:
wordbank[word] = 1
else:
wordbank[word] += 1
sortedwords = sorted(wordbank.items(), key=operator.itemgetter(1))
for word in sortedwords:
print (word[1], word[0])
最佳答案
像这样:
在此示例中,您可以根据需要创建文件列表-可能正在使用glob或任何其他方式。如果您需要帮助-请告知创建条件。
filelist = ['D.txt','E.txt']
wordbank = {}
for file in filelist:
article_one = re.findall('\w+', open(file,).read().lower())
for word in article_one:
word = word.lower().strip(string.punctuation)
if word not in wordbank:
wordbank[word] = 1
else:
wordbank[word] += 1
sortedwords = sorted(wordbank.items(), key=operator.itemgetter(1))
for word in sortedwords:
print (word[1], word[0])
关于python - Python:如何在代码中包含多个文本文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37422127/