背景介绍
在开发程序时,统计文本中的单词出现次数是一项常见的任务。本项目通过读取用户输入的文本文件,统计其中每个单词的出现次数,并输出结果。程序要求用户输入文件路径,并输出统计结果,确保程序在1~3天内完成实现,掌握文件读取和数据结构的统计知识。
思路分析
- 文件操作
使用open()函数读取文件内容,通过readlines()或read()逐行读取数据,避免逐行写入文件,提高效率。
例如:with open(file_path, 'r') as f: content = f.read() - 单词分割与统计
使用split()方法将文本拆分为单词列表,自动处理空格、换行符等特殊字符。例如:words = content.split() - 频率统计
使用字典(dictionary)统计每个单词的出现次数,支持空值(空字符串视为0)的处理。from collections import defaultdict word_counts = defaultdict(int) for word in words: word_counts[word] += 1 - 输出结果
将统计结果以指定格式输出,确保所有单词按顺序排列,并用空格分隔。例如:print("单词:", word_counts.keys(), end=" ") print("(", word_counts.values(), end=")")
代码实现
from collections import defaultdict
def count_words_in_file(file_path):
# 读取文件内容
with open(file_path, 'r') as f:
content = f.read()
# 将文本拆分为单词列表
words = content.split()
# 统计单词出现次数
word_counts = defaultdict(int)
for word in words:
word_counts[word] += 1
# 输出统计结果
print("单词:", word_counts.keys(), end=" ")
print("(", word_counts.values(), end=")")
return word_counts
# 示例使用
file_path = input("请输入文件路径:") # 输入文件路径
word_counts = count_words_in_file(file_path)
总结
本项目通过基本的文件读取和统计实现,展现了Python在处理文本内容时的灵活性。核心知识点包括:
1. 文件操作:使用open()和readlines()读取文本内容。
2. 数据结构:使用字典统计单词频率。
3. 输出格式化:通过列表推导式和空格分隔实现结果输出。
该程序可以在本地环境运行,无需依赖大型框架或外部服务,适合中级程序员在1~3天内完成实现,具备良好的学习价值。