刚刚在写文章时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读取一个文本文件并统计词频的更多相关文章

  1. asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现

    asp.net 读取一个文本文件,并输出到网页显示 通过 一般处理程序实现 用这个可以做模板首页进行输出,也可以自已自定义进行扩展 //得到读取到的文本到string中 string resultTe ...

  2. python读取一个文件的每一行判断是否为素数,并把结果写到另一个文件中

    刚刚学习python的菜鸟,这道题包括:文件的读写,python的参数调用,异常的使用,函数的使用 创建一个文本文件inti_prime.txt 执行命令:python Prime.py init_p ...

  3. python 读取一个文件夹下的所jpg文件保存到txt中

    最近需要使用统计一个目录下的所有文件,使用python比较方便,就整理了一下代码. import os def gci(filepath): files = os.listdir(filepath) ...

  4. Python读取一个目录下的所有文件

    #!/usr/bin/python # -*- coding:utf8 -*- import os allFileNum = 0 def printPath(level, path): global ...

  5. python 读取一个目录下的所有目录和文件

    #!/usr/bin/python # -*- coding:utf8 -*- import os allFileNum = 0 def printPath(level, path): global ...

  6. Java读取一个文本文件拼接成一个字符串(readFileToString)

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.I ...

  7. python读取一个英文文件,并记录每个单词出现的次数,降序输出

    对文中出现的句号,逗号和感叹号做了相应的处理 sorted排序函数用法: 按照value值降序排列: sorted(dict.items(),key=lambda k:k[1],reverse=Tru ...

  8. 面试题-python 如何读取一个大于 10G 的txt文件?

    前言 用python 读取一个大于10G 的文件,自己电脑只有8G内存,一运行就报内存溢出:MemoryError python 如何用open函数读取大文件呢? 读取大文件 首先可以自己先制作一个大 ...

  9. Python 读取图像文件的性能对比

    Python 读取图像文件的性能对比 使用 Python 读取一个保存在本地硬盘上的视频文件,视频文件的编码方式是使用的原始的 RGBA 格式写入的,即无压缩的原始视频文件.最开始直接使用 Pytho ...

随机推荐

  1. Matplotlib 饼图

    章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...

  2. 强制找回GitLab管理员账户密码的方法

    为了开发运维工具,我们采用自行搭建的GitLab来管理所有代码.悲催的是最近忘记了管理员账户的密码,而且没有邮件服务器,因此无法接收密码找回的邮件,导致无法新建用户或者项目,这样一来,岂不就成为了一个 ...

  3. 洛谷P2296 寻找道路

    \(\Large\textbf{Description:} \large {在有向图 G 中,每条边的长度均为 1,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件:}\) \ ...

  4. push 和 append 以及appendchild 用法和区别

    push() 给数组添加元素,并且返回数组长度 如 : arr.push('a') append() 是jq写法,添加节点到指定父级节点的子节点列表末尾 appendchild() 是append原生 ...

  5. MySQL 中的数据库名称、数据表名称、字段名称

    如何查询Oracle,Sql Server,MySQL 中的数据库名称.数据表名称.字段名称 分类: Database2012-09-24 22:16 7034人阅读 评论(0) 收藏 举报 数据库s ...

  6. 云时代架构阅读笔记十一——数据库SQL优化

    网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 ...

  7. vue题型

    一  v-show和v-if区别 个人理解 相同:v-show和v-if都能控制元素的显示和隐藏.两个都是开关. 区别: 1.v-if 是懒加载,是有条件的渲染,它会确保在切换过程中添加或者删除元素. ...

  8. Java连载79-Calendar解析

    一. Calendar解析 package com.bjpowernode.java_learning; import java.util.Date; import java.text.ParseEx ...

  9. IIS7/8 HTTP Error 500.19 错误 0x80070021 错误代码:0x8007000d

    nopCommerce versions 4.20 的安装环境是 dotnet-hosting-2.2.0-win.exe .net core项目iis10上出现 HTTP 错误 500.19,错误代 ...

  10. ucosiii 移植

    最近想在 f429 上面使用 mdk526 版本的 IDE,配合 HAL 和ucosiii.考虑到的方法是对比 v7 开发板的 ucosiii 和裸机程序,找出需要修改的地方,然后对比 v6 开发板的 ...