Python | 分析txt文档特定词汇的词频,以《天龙八部》为例
2022/2/4 20:14:34
本文主要是介绍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 | 分析txt文档特定词汇的词频,以《天龙八部》为例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用FastAPI掌握Python异步IO:轻松实现高并发网络请求处理
- 2025-01-02封装学习:Python面向对象编程基础教程
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型