用Python读取一个文本文件并统计词频
刚刚在写文章时360浏览器崩溃了,结果内容还是找回来了,感谢博客园的自动保存功能!!!
------------恢复内容开始------------
最近在学习Python,自己写了一个小程序,可以从指定的路径中读取文本文档,并统计其中各单词出现的个数并打印
import os
#此方法用于创建文件夹及文件
def createFile(fileName,content,filePath=r'd:/PythonExercise/'):
# 创建文件夹
os.mkdir(filePath)
fullPath=filePath+fileName
f=open(fullPath,'w')
f.write(content)
f.close
#将下面一句话写入指定的文件
createFile('test.txt',"Life is short,so let's just enjoying Python!") #此方法用于读取文件并统计词频
def getWordsFrequency(fullFilePath=r'd:/PythonExercise/test.txt'):
f=open(fullFilePath,'r')
# 读取内容,并以空格分隔,split中如果不传参,默认为空格,以下适用于英文
tmp=f.readline().split()
# 以下适用于中文,由于中文汉字之间没有空格,读出来整体会是个str,所以要用list()转换成以单个汉字为内容的list
# tmp=list(f.readline()) f.close()
print(tmp)
#标点符号集
punctuation='''~!@#$%^&*()_+-[]{};:,./?"'''
#如果只是以空格分隔,会得到一些单词和标点的组合,如“if,”not!"之类的,遍历list并将其中含有标点的内容分隔,去掉原内容并将分割后的list加在原list后
for i in tmp:
for p in punctuation:
if p in i:
tmp1=i.split(p)
tmp.remove(i)
tmp.extend(tmp1)
#将空元素''去掉并将原有单词中所有字母转换成小写
for j in tmp:
if j=='':
# print("let's remove null")
tmp.remove(j)
else:
# print("let's get lowers")
tmp[tmp.index(j)]=j.lower()
# tmp.replace(j,j.lower())
#上面的if语句中已经去除过''字符,但不知为什么,最后一个去不掉,因此再去除一遍
while tmp.count('')!=0:
tmp.remove('')
# print(tmp.count(''))
# print('tmp after lower case',tmp)
#将处理后的单词列表去重并转化为tuple,方便后面使用
keys=tuple(set(tmp))
print(keys)
#生成一个和上面keys,即去重后的单词的元组长度相同的list,并先赋初值为0,方便后续统计词频
freq=list(0*i for i in range(len(keys)))
# print(freq)
#从keys中获取单词,并在tmp中统计出现的次数,将次数赋给freq中的元素,由于freq长度和keys一样,所以freq的序号可以和keys一一对应,方便后面组成字典
for words in keys:
freq[keys.index(words)]=tmp.count(words)
# print(freq)
#新建一个字典
freqDict={}
#将keys批量导入成为字典的键
freqDict=dict.fromkeys(keys)
#此时如果打印freqDict可以看到它的值全为None
# print(freqDict)
#将上面和和keys一一对应的freq的值赋给freqDict中对应的键
for words in keys:
# print(freqDict[words])
freqDict[words]=freq[keys.index(words)]
print(freqDict)
return freqDict
运行该函数就可以以字典的形式打印出词频
getWordsFrequency() 以下语句是从上面读出的单词中随机抽10个打印出来
wordSet=list(getWordsFrequency().keys())
#print(wordSet)
import random as r
抽取10个不同的元素,此方法随机数可以去重
randomWords=r.sample(wordSet,10)
用下面三行也可以抽出10个单词,但可能会有重复值
# randomWords=[]
# for i in range(10):
# randomWords.append(r.choice(wordSet))
print(randomWords)
程序输出的结果
(1)从bing.com的国际版随意一条热搜中选取了一段新闻并保存到test.txt中,运行结果如下
{'orbit': 1, 'hanging': 2, 'another': 1, 'pretty': 2, 'planets': 2, 'planet': 2, 'of': 2, 'system': 2, 'a': 4, 'rings': 1, 'two': 1, 'there’s': 1, 'life': 1, 'claim': 1, 'features': 1, 'moons': 3, 'both': 1, 'conditions': 1, 'means': 1, 'survey': 1, 'moon': 3, 'chance': 1, 'possible': 1, 'with': 1, 'our': 2, 'body': 1, 'have': 3, 'cnet': 1, 'is': 1, 'uranus': 1, 'red': 1, 'jupiter': 1, 'could': 2, 'earth’s': 2, 'reports': 1, 'several': 1, 'main': 1, 'be': 1, 'are': 1, 'which': 2, 'that’s': 1, 'fame': 1, 'sky': 1, 'earth': 2, 'gravity': 1, 'while': 1, 'place': 1, 'being': 1, 'call': 1, 'spot': 1, 'famous': 2, 'eyes': 1, 'it’s': 1, 'an': 1, 'for': 4, 'that': 3, 'right?': 1, 'solar': 2, 'distinctive': 1, 'its': 5, 'no': 1, 'orbiting': 1, 'has': 4, 'special': 1, 'mercury': 1, 'astronomers': 1, 'mini': 1, 'wondrous': 1, 'human': 1, 'now': 1, 'catalina': 1, 'asteroid': 1, 'single': 1, 'rock': 1, 'this': 1, 'cool': 1, 'and': 3, 'would': 1, 'all': 1, 'on': 1, 'Tuscon': 1, 'out': 2, 'at': 1, 'to': 2, 'az': 1, 'but,': 1, 'saturn': 1, 'use': 1, 'in': 5, 'it': 2, 'many': 1, 'the': 3, 'make': 1, 'home': 1, 'like': 1, 'perfect': 1, 'only': 1} ['to', 'system', 'reports', 'it', 'would', 'no', 'are', 'pretty', 'all', 'make'] (2)从新浪新闻中选取了一条,把其中第二段并保存到test.txt中,运行结果如下
{'闻': 1, '开': 1, '家': 2, '表': 1, '管': 1, '生': 4, '务': 1, '会': 1, '疫': 1, '系': 1, '物': 3, '非': 1, '了': 1, '院': 1, '以': 1, '法': 1, '日': 1, '施': 2, '格': 1, '工': 1, '最': 1, '控': 2, '取': 1, ',': 3, '措': 1, '布': 1, '严': 2, '新': 1, '保': 1, '厉': 1, '作': 1, '。': 2, '召': 1, '来': 1, '打': 1, '野': 3, '局': 2, '和': 2, '动': 3, '护': 1, '王': 1, '示': 1, '林': 2, '实': 1, '副': 1, '场': 1, '2': 1, '贸': 1, '况': 1, '绍': 1, '长': 1, '维': 1, '情': 2, '司': 2, '坚': 1, '击': 1, '防': 1, '决': 1, '胜': 1, '制': 1, '联': 2, '列': 1, '草': 2, '机': 1, '市': 1, '易': 1, '介': 1, '缔': 1, '的': 1, '植': 1, '发': 2, '国': 3, '7': 1, '为': 1}
<class 'list'>
['严', '联', '的', '2', '动', ',', '和', '院', '物', '施'] (3)将python之道的诗保存到test.txt中,运行结果如下
{"aren't": 1, 'implicit': 1, 'right': 1, 'practicality': 1, 'nested': 1, 'although': 3, 'beautiful': 1, 'break': 1, 'errors': 1, 'of': 3, 'refuse': 1, 'a': 2, 'dense': 1, 'more': 1, 'easy': 1, "you're": 1, 'sparse': 1, 'peters': 1, 'do': 2, 'may': 2, 'explicit': 1, 'implementation': 2, 'often': 1, 'great': 1, 'pass': 1, 'those': 1, 'purity': 1, 'is': 10, 'ambiguity': 1, 'face': 1, 'be': 3, 'by': 1, 'are': 1, 'silently': 1, 'cases': 1, 'bad': 1, 'idea': 3, 'if': 2, "it's": 1, 'not': 1, 'counts': 1, 'zen': 1, 'readability': 1, 'that': 1, 'honking': 1, 'temptation': 1, 'than': 8, 'ugly': 1, 'Dutch': 1, "let's": 1, 'guess': 1, 'namespaces': 1, 'special': 2, 'better': 8, 'now': 1, 'good': 1, 'complicated': 1, 'now.': 1, 'simple': 1, 'complex': 2, 'there': 1, 'python': 1, 'first': 1, 'way': 2, 'and': 1, 'beats': 1, 'hard': 1, 'explicitly': 1, 'silenced': 1, 'at': 1, 'to': 5, 'obvious': 2, 'never': 3, 'tim': 1, 'in': 1, 'one': 3, 'explain': 2, 'unless': 2, 'enough': 1, 'preferably': 1, 'should': 2, 'it': 2, 'the': 6, 'flat': 1, 'rules': 1, 'only': 1}
<class 'list'>
['of', 'honking', 'preferably', 'by', "you're", 'complicated', 'sparse', 'and', 'pass', 'enough']
------------恢复内容结束------------
为了防止链接失效,手动将1、2、3中的三段文本放在下面
1、
Several planets in our solar system are famous for distinctive features. Saturn has its wondrous rings and Jupiter has its famous red spot, while Uranus has its many moons and planets like Mercury have no moons at all. Earth’s main claim to fame is it being the only planet in the solar system with the perfect conditions for human life and a single moon, both of which make it a pretty special place for use to call home. But, there’s a chance that Earth could have another moon hanging out in its orbit for now. CNET reports that Catalina Sky Survey astronomers in Tuscon, AZ has its eyes on an asteroid hanging out in Earth’s gravity. It’s possible that this body of rock could be a mini-moon orbiting our planet, which means Earth would have two moons. That’s pretty cool, right?
2、
国务院联防联控机制27日召开新闻发布会,介绍坚决取缔和严厉打击非法野生动物市场和贸易工作情况。国家林草局野生动植物保护司副司长王维胜表示,疫情发生以来,国家林草局实施了最为严格的野生动物管控系列措施。
3、
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
用Python读取一个文本文件并统计词频的更多相关文章
- asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现
asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现 用这个可以做模板首页进行输出,也可以自已自定义进行扩展 //得到读取到的文本到string中 string resultTe ...
- python读取一个文件的每一行判断是否为素数,并把结果写到另一个文件中
刚刚学习python的菜鸟,这道题包括:文件的读写,python的参数调用,异常的使用,函数的使用 创建一个文本文件inti_prime.txt 执行命令:python Prime.py init_p ...
- python 读取一个文件夹下的所jpg文件保存到txt中
最近需要使用统计一个目录下的所有文件,使用python比较方便,就整理了一下代码. import os def gci(filepath): files = os.listdir(filepath) ...
- Python读取一个目录下的所有文件
#!/usr/bin/python # -*- coding:utf8 -*- import os allFileNum = 0 def printPath(level, path): global ...
- python 读取一个目录下的所有目录和文件
#!/usr/bin/python # -*- coding:utf8 -*- import os allFileNum = 0 def printPath(level, path): global ...
- Java读取一个文本文件拼接成一个字符串(readFileToString)
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...
- python读取一个英文文件,并记录每个单词出现的次数,降序输出
对文中出现的句号,逗号和感叹号做了相应的处理 sorted排序函数用法: 按照value值降序排列: sorted(dict.items(),key=lambda k:k[1],reverse=Tru ...
- 面试题-python 如何读取一个大于 10G 的txt文件?
前言 用python 读取一个大于10G 的文件,自己电脑只有8G内存,一运行就报内存溢出:MemoryError python 如何用open函数读取大文件呢? 读取大文件 首先可以自己先制作一个大 ...
- Python 读取图像文件的性能对比
Python 读取图像文件的性能对比 使用 Python 读取一个保存在本地硬盘上的视频文件,视频文件的编码方式是使用的原始的 RGBA 格式写入的,即无压缩的原始视频文件.最开始直接使用 Pytho ...
随机推荐
- 苹果vs中国竞争者:瘦死的骆驼比马大?
前不久,苹果调整2019年第一财季的营收指引,预计第一季度毛利率为38%,相关收入大约为55亿美元,全年总体营收约为840亿美元,运营开支约为87亿美元.针对2019年的运营状况,库克亲自给投资者写了 ...
- SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
[oracle@jtwy02 ~]$ sqlplus '/as sysdba' SQL*Plus: Release 11.2.0.4.0 Production on Sat Oct 13 14:14: ...
- Java笔记--常用类
1.String类: --使用Unicode字符编码,一个字符占两个字节: --String类是一个final类,代表不可变的字符序列: --字符串是不可变的,一个字符串对象一旦被配置,其内容是不可变 ...
- 005、Java中使用文档注释
01. 代码如下: package TIANPAN; /** * 此处为文档注释 * @author 田攀 微信382477247 */ public class TestDemo { public ...
- ApplicationListener监听使用ContextRefreshedEvent事件类型会触发多次
@Componentpublic class TestApplicationListener implements ApplicationListener<ContextRefreshedEve ...
- liunx笔记
Zolertia IPv6/6LoWPAN Ubidots client Son Han Border Router with Raspberry Pi for LLN with TelosBs Co ...
- Python 比较 相等性 真值
1 == 操作符测试 值 的相等性: is 测试对象的一致性.注意短字符串的is相等性测试,PVM会缓存短字符串,s1 is s2 将返回true. 2 false:"", [], ...
- kafka创建topic,生产和消费指定topic消息
启动zookeeper和Kafka之后,进入kafka目录(安装/启动kafka参考前面一章:https://www.cnblogs.com/cici20166/p/9425613.html) 1.创 ...
- Maven的安装和创建项目的过程
一.下载Maven包和配置环境变量 1.将下载好的maven包放到一个目录中:目录中不能有汉字和空格 2.配置环境变量 3.配置path路径 二.配置阿里云私服 1.找到setting目录,配置下载j ...
- 二、【未来】React环境安装:npx
搭建React的开发环境的第二种方法(新-未来推荐): https://reactjs.org/docs/create-a-new-react-app.html 一. npx简介: 1. npm v5 ...