文本文件中的出现次数[关闭]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文本文件中的出现次数[关闭]相关的知识,希望对你有一定的参考价值。
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()
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
以上是关于文本文件中的出现次数[关闭]的主要内容,如果未能解决你的问题,请参考以下文章