Files
Feature-Extraction/txt_counter.py

27 lines
754 B
Python

from collections import Counter
def count_lines(file_path):
# 读取文件并统计每一行的出现次数
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
# 使用Counter统计每一行的出现次数
line_counts = Counter(lines)
# 按出现次数从高到低排序
sorted_line_counts = line_counts.most_common()
return sorted_line_counts
def main(file_path):
sorted_line_counts = count_lines(file_path)
# 打印结果
for line, count in sorted_line_counts:
print(f'{line.strip()} - Count: {count}')
if __name__ == "__main__":
# 指定你的txt文件路径
file_path = "D://python_study//big_boss//doc//output//all_processed.txt"
main(file_path)