首页 > 解决方案 > 在txt文件Python中逐行查找单词的频率(如何正确格式化)

问题描述

我正在尝试制作一个简单的程序,可以逐行查找文本文件中出现的频率。我让它正确输出所有内容,除非文本文件中的一行上有多个单词。(下面有更多信息)

文本文件如下所示:

Hello
Hi
Hello
Good Day
Hi 
Good Day
Good Night

我希望输出是:(不必是相同的顺序)

Hello: 2
Hi: 2
Good Day: 2
Good Night: 2

它当前输出的内容:

Day: 2
Good: 3
Hello: 2
Hi: 2
Night: 1

我的代码:

file = open("test.txt", "r") 
text = file.read() #reads file (I've tried .realine() & .readlines()


word_list = text.split(None)
word_freq = {}  # Declares empty dictionary

for word in word_list:
    word_freq[word] = word_freq.get(word, 0) + 1
    keys = sorted(word_freq.keys())

for word in keys:
    final=word.capitalize()
    print(final + ': ' + str(word_freq[word])) # Line that prints the output

标签: pythonlistfile

解决方案


不是将文本按 None 拆分,而是按每个换行符拆分它,这样您就可以将每一行放入一个列表中。

file = open("test.txt", "r") 
text = file.read() #reads file (I've tried .realine() & .readlines()

word_list = text.split('\n')
word_freq = {}  # Declares empty dictionary

for word in word_list:
    word_freq[word] = word_freq.get(word, 0) + 1
    keys = sorted(word_freq.keys())

for word in keys:
    final=word.capitalize()
    print(final + ': ' + str(word_freq[word])) # Line that prints the output


推荐阅读