Python | 分析txt文档特定词汇的词频,以《天龙八部》为例
作者:互联网
首先要将txt文件另存为utf-8格式的文件,然后才能进行处理。
import re
from pymysql import DataError
with open (r"C:\Users\ausa\Desktop\tianlong.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))
标签:为例,Python,len,re,词频,乔峰,txt,data,findall 来源: https://blog.csdn.net/wh90306/article/details/122784472