首页 > 解决方案 > 显示字典Python中查询文件中每行单词的次数

问题描述

我有一个 docs.txt 作为文件,注意分开的三行:

joyously urgently truthfully seemingly broadly urgently knowingly urgently steadily
joyously urgently truthfully seemingly rigidly broadly rigidly suddenly healthily commonly often
tremendously totally steadily sharply totally

我有一个带有此文件的querys.txt:

urgently
rigidly suddenly
totally steadily

和代码:

dictionary = {}
    angledictionary={}
    document= open('docs.txt', 'r')
    for line in document:
        lined=line.split()
        for word in lined:
            if word not in dictionary.keys():
                dictionary[word]=0
            dictionary[word]+=1
    dictionary=dict.fromkeys(dictionary,0)
    with open("queries.txt", "r") as open_queries:
        searchquery = open_queries.read().split("\n")
    with open('docs.txt', 'r') as openrelevancy:
        words = openrelevancy.read().split("\n")
    for query in searchquery:
        print('Query:', query)
        relevant = []
        line_number = 0
        for word in words:
            line_number += 1
            if query in word:
                relevant.append(line_number)
        print('Relevant Documents:', *relevant)

现在,每行的字典字数为 0,我正在尝试这样做: FOR LINE 1:

{'joyously': 1, 'urgently': 3, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'knowingly': 1, 'steadily': 1, 'rigidly': 0, 'suddenly': 0, 'healthily': 0, 'commonly': 0, 'often': 0, 'tremendously': 0, 'totally': 0, 'sharply': 0}

对于第 2 行:

{'joyously': 1, 'urgently': 1, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'knowingly': 1, 'steadily': 1, 'rigidly': 2, 'suddenly': 1, 'healthily': 1, 'commonly': 1, 'often': 1, 'tremendously': 0, 'totally': 0, 'sharply': 0}

对于第 3 行:

{'joyously': 0, 'urgently': 0, 'truthfully': 0, 'seemingly': 0, 'broadly': 0, 'knowingly': 0, 'steadily': 0, 'rigidly': 0, 'suddenly': 0, 'healthily': 0, 'commonly': 0, 'often': 0, 'tremendously': 1, 'totally': 2, 'sharply': 1}

我该如何解决这个问题?

标签: python

解决方案


下面的代码将计算给定文件中的每个单词。

import os

def to_dict(file_path, delimiter=" "):
    dict_list = []  # Each index is a line
    if os.path.exists(file_path):
        with open(file_path, 'rb') as in_file:
            for line in in_file.readlines():  # Grab each line as a list
                line = line.split(delimiter)  # Split at our delim
                new_dict = {}
                for word in line:  # Code to count each index
                    word = word.rstrip()  # Remove formatting
                    if word in new_dict:
                        new_dict[word] += 1
                    else:
                        new_dict[word] = 1
                dict_list.append(new_dict)
        return dict_list
    else:
        print("{} Does not exist. Check the path and try again".format(file_path))

dict_count = to_dict(the_file_path, " ")
if dict_count:  # We found and converted file to a dict
    # function to query against words

推荐阅读