繁体   English   中英

计算目录中 a.txt 文件中特定单词的出现次数

[英]counting appearances of a specific word in a .txt file in a directory

我正在尝试编写一个 function 来计算给定目录中每个文件中特定单词的出现次数。 希望在 python 中执行此操作。 我有些不知所措。

在文件.py

import os
files_and_directories = os.listdir("./files/")
prefix = './files/' 
iterator = 0
for file in files_and_directories: 
    with open(f'{prefix}/{file}', 'r') as f: 
        for line in f:
            for word in line.split():
                if word == "Line":
                    print(word)
                    iterator += 1

print(iterator)



Of course, change the 'if word == "Line"' you whatever you want.

这种方法返回一个字典,其中包含path指定的文件夹中每个文本文件的条目,其中键是文件名,值是字典,单词作为键,计数作为值。

import os
from collections import Counter

def fun(path):
    res = {}
    for filename in os.listdir(path):
        if filename.endswith('.txt'):
            filepath = os.path.join(path, filename)
            with open(filepath, 'r') as f:
                words = f.read().split()
            res[filename] = dict(Counter(words))
    return res

您可以使用glob和下面的代码创建一个包含文件名作为键和单词出现计数的字典:

from glob import glob

word = 'apple'
result = {}
for filename in glob('./*.txt'):
  with open(filename, 'r') as f:
    result[filename] = len([w for w in f.read().replace('\n', ' ').split(' ') if w == word])

根据需要更改 word 变量的值

因此,如果当前文件夹中有两个 txt 文件的内容:

文件1.txt

apple fruit apple

文件2.txt

apple fruit orange

结果字典将是:

{'./file2.txt': 1, './file1.txt': 2}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM