文本文件中的出现次数[关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文本文件中的出现次数[关闭]相关的知识,希望对你有一定的参考价值。

我有200个文本文件,希望计算每个文本文件中每个单词的出现次数,并每次都保存到不同的文本文件中

count = for w in open('Vimle_P_006_0_E1900.txt',errors='ignore',encoding='utf8').read().split(): if w in count: count[w] += 1 else: count[w] = 1 for word, times in count.items(): out_put = str(word) + " was found = " + str(times) + " times" + "\n" with open('Test.txt','a',encoding='utf8') as f: f.write(out_put) f.close()

答案
假设您有200个文本文件的列表list_of_files,您可以这样做:

def write_to_files(infile): count = for w in open(infile,errors='ignore',encoding='utf8').read().split(): #open the file if w in count: count[w] += 1 else: count[w] = 1 for word, times in count.items(): out_put = str(word) + " was found = " + str(times) + " times" + "\n" with open(infile+"_out.txt",'a',encoding='utf8') as f: #write to a different file each time f.write(out_put) for file in list_of_files: write_to_files(file) #iterate over the list of files and call the function for each file separately

以上是关于文本文件中的出现次数[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

打印文件中每个单词的出现次数[关闭]

计算一个单词在文本文件中出现的次数

计算纯文本文件中字符的出现次数

如何使用 UNIX shell 计算一个字母在文本文件中出现的次数?

计算特定单词在 C++ 文本文件中出现的次数

查找哈希集中每个单词在文本文档中出现的次数