我正在尝试创建一个程序,该程序接受给定路径中的所有文本文件,并将所有字符串保存在一个列表中:
import os
import collections
vocab = set()
path = 'a\\path\\'
listing = os.listdir(path)
unwanted_chars = ".,-_/()*"
vocab={}
for file in listing:
#print('Current file : ', file)
pos_review = open(path+file, "r", encoding ='utf8')
words = pos_review.read().split()
#print(type(words))
vocab.update(words)
pos_review.close()
print(vocab)
pos_dict = dict.fromkeys(vocab,0)
print(pos_dict)
输入
file1.txt: A quick brown fox.
file2.txt: a quick boy ran.
file3.txt: fox ran away.
输出
A : 2
quick : 2
brown : 1
fox : 2
boy : 1
ran : 2
away : 1
到目前为止,我还能够编一本这些字符串的字典。但现在不确定如何使键、值对字符串及其频率在所有文本文件中结合起来。你知道吗
这同样有效
希望这有帮助
使用^{} :
Counter
是用于计算iterables的dict
子类数据
t1.txt
,t2.txt
&;t3.txt
代码:
获取文件:
收集字数:
clean_str
,用于清理每一行文本列表
words
:相关问题 更多 >
编程相关推荐