• 大小: 401KB
    文件类型: .zip
    金币: 2
    下载: 2 次
    发布日期: 2021-11-12
  • 语言: Python
  • 标签: Python  文件读取  

资源简介

file=open("考研英语真题.txt",'r',encoding='UTF-8') 只要把文件放在同一目录下,在上面这一行中改一下文件名就行。 考研时候感觉辅导书上的高频词汇有点问题,就写了这么个程序^_^

资源截图

代码片段和文件信息

import re
#coding=utf-8

file=open(“考研英语真题.txt“‘r‘encoding=‘UTF-8‘)
file_context=file.read()
#print (file_context)
file.close()
print (‘file read over and colsed‘)
print (‘##‘*40)

word_dric={}
all_words_list=[]


all_words_list =re.findall (r‘.+?\b‘file_context) #find all the words 
#print (all_words_list)
print (‘find all the words‘)
print (‘##‘*40)

for word in all_words_list:
    if word in word_dric:
        word_dric[word]=word_dric[word]+1
    else: 
        word_dric[word]=1
print (‘words occurrences count over‘)
print (‘##‘*40)


sortedlist =sorted(word_dric.items()key = lambda x:x[1]reverse = True)
#print (sortedlist)
print(‘sorted over‘)
print (‘##‘*40)

file=open(“process_result.txt“‘w‘encoding=‘UTF-8‘)
for item in sortedlist:
    file.write(str(item))
    file.write(‘\n‘)
file.close()
print (‘result write into txt over‘)

‘‘‘
file=open(“process_result.txt“‘w‘encoding=‘UTF-8‘)
for word in word_dric:
    file.write(word)
    file.write(‘       :‘)
    file.write( str(word_dric[word]) )
    file.write(‘\n‘)
file.close()
print (‘result write over‘)

‘‘‘





 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        1159  2019-01-21 12:43  count_words_occurrences.py
     文件     1083609  2019-01-21 03:34  考研英语真题.txt

评论

共有 条评论