Build a dictionary from .txt files analysis

Tags: , ,



I have a basic program that can count the number of words in a given text file. I am trying to turn this into a program that can take in several different .txt files, with an arbitrary amount of keywords within those file analyzed, and output a dictionary within a list of the results (or similar object).

The output I am looking for is a list of dictionaries wherein the list keys are the names of the .txt files in the filenames list, and the dictionary keys-values are the arbitrary words within the first function and their words counts, respectively.

I have two function that I have created and cannot seem to get any out whatsoever – which means that somethin n.

Code:

def word_count(filename, *selected_words):
    """Count the approximate number of words in a file."""
    with open(filename,"r",encoding='utf-8') as f_obj:
            contents = f_obj.read()
    filename = {} 
    filename['document'] = filename 
    filename['total_words'] = len(contents.split()) 
       
    for word in selected_words:
        count =  contents.lower().count(word)
        filename[word] = count
    return filename

def analysis_output():
    for file in files:
        word_count(file, 'the', 'yes') #WORD_COUNT FUNCTION 

files = ['alice.txt', 'siddhartha.txt', 
'moby_dick.txt', 'little_women.txt']

analysis_output()

When I run this, I am not getting any output – no errors telling me the code has run (likely improperly). Any advice on how to turn this into a a list of dictionaries is helpful!

Answer

You simply forgot to define a variable to receive the output from word_count. In fact, you can do it this way:

def word_count(filename, *selected_words):
    """Count the approximate number of words in a file."""
    with open(filename,"r",encoding='utf-8') as f_obj:
            contents = f_obj.read()
    results_dict = {} 
    results_dict['document'] = filename 
    results_dict['total_words'] = len(contents.split()) 
       
    for word in selected_words:
        count =  contents.lower().count(word)
        results_dict[word] = count
    return results_dict

def analysis_output():
    output = []
    for file in files:
        output.append(word_count(file, 'the', 'yes')) #WORD_COUNT FUNCTION 
    return output

files = ['alice.txt', 'siddhartha.txt', 
'moby_dick.txt', 'little_women.txt']

final_result = analysis_output()


Source: stackoverflow