欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Python|分析txt文档特定词汇的词频,以《天龙八部》为例

时间:2023-05-31

首先要将txt文件另存为utf-8格式的文件,然后才能进行处理。 

import refrom pymysql import DataErrorwith open (r"C:UsersausaDesktoptianlong.txt","rb") as f: data = f.read().decode()pat11 = "乔峰"pat12 = "乔大哥"pat13 = "萧峰"pat14 = "萧大哥"pat21 = "段誉"pat22 = "段公子"pat31 = "虚竹"pat32 = "小和尚"n11 = re.findall(pat11,data)n12 = re.findall(pat12,data)n13 = re.findall(pat13,data)n14 = re.findall(pat14,data)n21 = re.findall(pat21,data)n22 = re.findall(pat22,data)n31 = re.findall(pat31,data)n32 = re.findall(pat32,data)print("乔峰出现的次数:",len(n11)+len(n12)+len(n13)+len(n14),"段誉出现的次数:",len(n21)+len(n22),"虚竹出现的次数:",len(n31)+len(n32))

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。