Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

python - Build a dictionary from .txt files analysis

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...