python - 在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
解决方案
不是将文本按 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
推荐阅读
- c# - 从 C# 中的返回对象中删除属性
- dialogflow-es - 如何结束我对助手的自定义操作?
- scala - 在 IntelliJ 的 Maven 项目中运行 scala 代码的 maven-scala-plugin 错误
- c# - UNITY 2D Dungeon Cralwer 从一个房间传送到另一个房间
- python - Cython 素数示例 - SyntaxError:无效语法
- javascript - HTML - 元素结束
- python - Python在相同的if语句中检查条件和安全性
- angular - Angular testing - 测试相互依赖的服务
- javascript - ANGULAR - 下面的 EventEmitter 服务是如何工作的?
- c++ - 将分钟转换为 24 小时格式