要检查文件中是否有重复的单词,可以使用多种编程语言来实现。以下是一个使用Python的示例,展示了如何读取文件、检查重复单词并输出结果。
def check_duplicate_words(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
words = content.split() # 将内容分割成单词列表
word_count = {} # 用于记录每个单词出现的次数
for word in words:
word = word.strip('.,!?:;').lower() # 去除标点符号并转换为小写
if word: # 确保单词不为空
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
duplicates = {word: count for word, count in word_count.items() if count > 1}
if duplicates:
print("文件中存在重复的单词:")
for word, count in duplicates.items():
print(f"{word}: {count} 次")
else:
print("文件中没有重复的单词。")
except FileNotFoundError:
print(f"文件 {file_path} 未找到。")
except Exception as e:
print(f"读取文件时发生错误: {e}")
# 使用示例
check_duplicate_words('example.txt')
open
函数读取文件内容。split
方法将内容分割成单词列表。word_count
记录每个单词出现的次数。FileNotFoundError
异常并提示用户。utf-8
)以避免读取错误。通过这种方式,可以有效地检查文件中的重复单词,并且代码具有良好的可读性和扩展性。
没有搜到相关的文章