Python实现一篇txt文章的词频统计:文件读取+字典

博客通过Python展示了如何统计txt文章的词频,包括文件读取和使用字典进行统计的方法。文中提及代码示例,并附带了一个dictTxt.txt文件的内容,该文件包含了一段演讲稿。
摘要由CSDN通过智能技术生成
上周tensorflow小组作业记录一下,初始代码来自周帜老师我们可爱的gg,我做了一些输出调整,并制作了原理讲解,以下:所见即所得。
操作:把txt文件和.py文件保存在一个文件夹下,接着复制粘贴代码就可以所见即所得啦
思路将在以下的图片中分步讲明,也可以拉到底直接看代码

在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述
代码:

#完成词频的统计,需要将dictTxt.txt文件和程序文件放在同一文件夹中
import operator
import os
#打开文件
speech_text=''
try:
	f = open(os.path.abspath('dictTxt.txt'),'r')
	speech_text = f.read()
	f.close()
except:
	print("File Open Error.")

print('Speech text:')
print(speech_text)

#将单词转换为小写
speech = speech_text.lower().split()

print('After lower()&split():')
print(speech)

# 利用字典进行处理
dic = {}
for word in speech:
	if word not in dic:
		dic[word]=1
	else:
		dic[word]+=1

print('Dic:')
print(dic)

#对字典中的进行排序
swd = sorted(dic.items(),key=operator.itemgetter(1),reverse=True)
print(swd)

我还写了另外一种代码写法,但是没有上一种好,也粘上来一下

#完成词频的统计,需要将dictTxt.txt文件和程序文件放在同一文件夹中
import operator
#打开文件
try:
	f = open('dictTxt.txt','r')
	speech_text = f.read()
finally:
	if f:
		f.close()
#将单词转换为小写
speech = speech_text.lower().split()
#print(speech)
# 利用字典进行处理
dic = {}
for word in speech:
	if word not in dic:
		dic.update({word:1})
	else:
		dic.update({word:dic[word]+1})

#对字典中的进行排序
swd = sorted(dic.items(),key=operator.itemgetter(1),reverse=True)
print(swd)

p.s.
附上dictTxt.txt文件内容:
My fellow citizens: I stand here today humbled by the task before us, grateful for the trust you’ve bestowed, mindful of the sacrifices borne by our ancestors.
I thank President Bush for his service to our nation – (applause) – as well as the generosity and cooperation he has shown throughout this transition.
Forty-four Americans have now taken the presidential oath. The words have been spoken during rising tides of prosperity and the still waters of peace. Yet, every so often, the oath is taken amidst gathering clouds and raging storms. At these moments, America has carried on not simply because of the skill or vision of those in high office, but because we, the people, have remained faithful to the ideals of our forebears and true to our founding documents.
So it has been; so it must be with this generation of Americans.
That we are in the midst of crisis is now well+++ understood. Our nation is at war against a far-reaching network of violence and hatred. Our economy is badly weakened, a consequence of greed and irresponsibility on the part of some, but also our collective failure to make hard choices and prepare the nat

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值