用Python读取一个文本文件并统计词频

刚刚在写文章时360浏览器崩溃了,结果内容还是找回来了,感谢博客园的自动保存功能!!!

------------恢复内容开始------------

最近在学习Python,自己写了一个小程序,可以从指定的路径中读取文本文档,并统计其中各单词出现的个数并打印

 1 import os
 2 #此方法用于创建文件夹及文件
 3 def createFile(fileName,content,filePath=r'd:/PythonExercise/'):
 4 #     创建文件夹
 5     os.mkdir(filePath)
 6     fullPath=filePath+fileName
 7     f=open(fullPath,'w')
 8     f.write(content)
 9     f.close
10 #将下面一句话写入指定的文件
11 createFile('test.txt',"Life is short,so let's just enjoying Python!")
12 
13 #此方法用于读取文件并统计词频
14 def getWordsFrequency(fullFilePath=r'd:/PythonExercise/test.txt'):
15     f=open(fullFilePath,'r')
16 #     读取内容,并以空格分隔,split中如果不传参,默认为空格,以下适用于英文
17      tmp=f.readline().split()
18 # 以下适用于中文,由于中文汉字之间没有空格,读出来整体会是个str,所以要用list()转换成以单个汉字为内容的list
19 #    tmp=list(f.readline())
20 
21     f.close()
22     print(tmp)
23 #标点符号集
24     punctuation='''~!@#$%^&*()_+-[]{};:,./?"'''
25 #如果只是以空格分隔,会得到一些单词和标点的组合,如“if,”not!"之类的,遍历list并将其中含有标点的内容分隔,去掉原内容并将分割后的list加在原list后
26     for i in tmp:
27         for p in punctuation:
28             if p in i:
29                 tmp1=i.split(p)
30                 tmp.remove(i)
31                 tmp.extend(tmp1)
32 #将空元素''去掉并将原有单词中所有字母转换成小写
33     for j in tmp:
34         if j=='':
35 #             print("let's remove null")
36             tmp.remove(j)
37         else:
38 #             print("let's get lowers")
39             tmp[tmp.index(j)]=j.lower()
40 #             tmp.replace(j,j.lower())            
41 #上面的if语句中已经去除过''字符,但不知为什么,最后一个去不掉,因此再去除一遍
42     while tmp.count('')!=0:
43         tmp.remove('')
44 #     print(tmp.count(''))
45 #     print('tmp after lower case',tmp)
46 #将处理后的单词列表去重并转化为tuple,方便后面使用
47     keys=tuple(set(tmp))
48     print(keys)
49 #生成一个和上面keys,即去重后的单词的元组长度相同的list,并先赋初值为0,方便后续统计词频
50     freq=list(0*i for i in range(len(keys)))
51 #     print(freq)
52 #从keys中获取单词,并在tmp中统计出现的次数,将次数赋给freq中的元素,由于freq长度和keys一样,所以freq的序号可以和keys一一对应,方便后面组成字典
53     for words in keys:
54         freq[keys.index(words)]=tmp.count(words)
55 #     print(freq)
56 #新建一个字典
57     freqDict={}
58 #将keys批量导入成为字典的键
59     freqDict=dict.fromkeys(keys)
60 #此时如果打印freqDict可以看到它的值全为None
61 #     print(freqDict)
62 #将上面和和keys一一对应的freq的值赋给freqDict中对应的键
63     for words in keys:
64 #         print(freqDict[words])
65         freqDict[words]=freq[keys.index(words)]
66     print(freqDict)
67     return freqDict
68 运行该函数就可以以字典的形式打印出词频
69 getWordsFrequency()
70 
71 以下语句是从上面读出的单词中随机抽10个打印出来
72 wordSet=list(getWordsFrequency().keys())
73 #print(wordSet)
74 import random as r
75 抽取10个不同的元素,此方法随机数可以去重
76 randomWords=r.sample(wordSet,10)
77 用下面三行也可以抽出10个单词,但可能会有重复值
78 # randomWords=[]
79 # for i in range(10):
80 #     randomWords.append(r.choice(wordSet))
81 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!

猜你喜欢

转载自www.cnblogs.com/flyingtester/p/12375408.html
今日推荐