Update task2.py

main
p6fxi93qh 2 months ago
parent d4c64867d4
commit 4d2f9fc128

@ -4,13 +4,11 @@ import os
import re import re
from openpyxl import Workbook from openpyxl import Workbook
def writeintxt (words,outpath): def writeintxt (words,outpath):
with open(outpath, 'w', encoding='utf-8') as output_file: with open(outpath, 'w', encoding='utf-8') as output_file:
for line in words: for line in words:
output_file.write(line) output_file.write(line)
def findfrecuency(txt_path,keywords,outpath): def findfrecuency(txt_path,keywords,outpath):
if os.path.exists(txt_path): if os.path.exists(txt_path):
# 读取原始文本文件,按行读入lines # 读取原始文本文件,按行读入lines
@ -51,7 +49,6 @@ def writeinexcel (keyword_count,savepath):
# 保存 Excel 文件 # 保存 Excel 文件
wb.save(savepath) wb.save(savepath)
if __name__ =='__main__': if __name__ =='__main__':
# 定义文件路径 # 定义文件路径
txt_path = 'd:\\学习\\软件工程\\swork\\res\\total300_3.txt'#总弹幕数据 txt_path = 'd:\\学习\\软件工程\\swork\\res\\total300_3.txt'#总弹幕数据
@ -59,7 +56,7 @@ if __name__ =='__main__':
# 初始化一个字典来存储每个关键词的计数 # 初始化一个字典来存储每个关键词的计数
keyword_count = {keyword: 0 for keyword in keywords} keyword_count = {keyword: 0 for keyword in keywords}
#定义保存位置 #定义保存位置
savepath = "d:\\学习\\软件工程\\swork\\res\\AI_dm_rank4.xlsx"#存放排名个数excel savepath = "d:\\学习\\软件工程\\swork\\res\\AI_dm_rank.xlsx"#存放排名个数excel
outpath = "d:\\学习\\软件工程\\swork\\res\\select_ai4.txt"#存放有含ai技术评论的评价txt outpath = "d:\\学习\\软件工程\\swork\\res\\select_ai.txt"#存放有含ai技术评论的评价txt
keyword_count = findfrecuency(txt_path,keywords,outpath) #得到排名字典 keyword_count = findfrecuency(txt_path,keywords,outpath) #得到排名字典
writeinexcel (keyword_count,savepath) #将排名写入excel writeinexcel (keyword_count,savepath) #将排名写入excel
Loading…
Cancel
Save