用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 ...
随机推荐
- 标准查询运算符---LINQ
Where 根据给定的谓词对序列进行过滤 Select 指定要包含一个对象或对象的一部分 SelectMany 一种查询类型,返回集合的集合.该方法将这些结果合并为一个单独的集合 Take 接受一个输 ...
- python里的def 方法中->代表什么意思?
功能注释 函数注释是关于用户定义函数使用的类型的完全可选元数据信息(请参阅PEP 3107和 PEP 484了解更多信息). 注释__annotations__ 作为字典存储在函数的属性中,对函数的任 ...
- 前端构建工具gulp超详细配置, 使用教程(图文)
流程 1. 输入命令(可以使用git bash或者命令控制台cmd) npm install -g gulp 安装全局gulp命令 2. 创建一个项目文件夹, 当前项目文件夹下输入命令npm init ...
- GoJS组织结构图
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Day 4 -E - Catenyms POJ - 2337
A catenym is a pair of words separated by a period such that the last letter of the first word is th ...
- leetcode102 Binary Tree Level Order Traversal
""" Given a binary tree, return the level order traversal of its nodes' values. (ie, ...
- delphi日期GMT格式
function TForm1.DateTimeToGMT(const DateTime: TDateTime): string;const WEEK: array[1..7] of PChar = ...
- ACM-挑战题之排列生成
题目描述:挑战题之排列生成 一自然数N,设N为3,则关于N的字典序排列为123,132,213,231,312,321.对于一个自然数N(1<= N <= 9 ) , 你要做的便是生成它的 ...
- Web UI设计师需要了解的用栅格化系统指导网页设计
出处:https://www.jianshu.com/p/9838f217f4f6 致敬,,, ---------------------------------------------------- ...
- Eclipse新建Maven中创建src文件夹报The folder is already a source folder错误解决办法
问题: 解决办法:右击项目->Build Path->Configure Build Path选择(missing)文件夹remove,然后重新New Source Folder