大数跨境
0
0

知识分享:1954-2021年政府工作报告关键词

知识分享:1954-2021年政府工作报告关键词 数据皮皮侠
2021-04-29
1
导读:点击 数据视觉PPman,选择加星标精彩内容不迷路大家好,我是数据视觉PPman上个月国家公布了《2021年

点击 数据视觉PPman选择加星标

精彩内容不迷路


大家好,我是数据视觉PPman

上个月国家公布了《2021年政府工作报告》,每年的政府工作报告都有不同的亮点和重点,那么今天数据视觉PPman就教大家如何爬取历年来的政府工作报告中出现最多的一些词语。

第一步,导入数据

import jiebaimport jieba.analyseimport xlwt

然后打开txt文件 并读取到res中

f = open(r'C:\Users\Lenovo\Desktop\1954-2021年政府工作报告\历年国务院政府工作报告19542017__中国政府.txt','r',encoding='utf-8')  res = f.read()

第二步,切割内容单元

按关键字——年国务院政府工作报告对内容进行切割,使得每一个元素表示一年政府工作报告全文,其中1997内正文中存在两年的国务院政府工作报告,因此需要将第29、30个元素合并
cut = res.split('年国务院政府工作报告')del(cut[0])c28=cut[28]c29=cut[29]c=str(c28+c29)del(cut[28])del(cut[29])cut.insert(29,c)


接着,创建年份列表


year1 =[]a=list(range(1954,1961))b=list(range(1978,2018))for i in a:    year1.append(i)for i in b:    year1.append(i)year1.insert(len(a),1964)year1.insert(len(a)+1,1975)year =[]for i in year1:    year.append(i)for i in list(range(2018,2022)):    year.append(i)

将切割好的内容逐年存放在txt文件中


for i in list(range(len(year1))):    year_num = year1[i]    word_i = cut[i]    file=r'C:\Users\Lenovo\Desktop\政府工作报告\{}政府工作报告.txt'.format(year_num)    with open(file,'w',encoding='utf-8') as f:        f.write(word_i)

第三步,统计词频

创建新的工作簿

wbk = xlwt.Workbook(encoding = 'ascii')
for i in year:        sheet_i = wbk.add_sheet('year_'+str(i))    word_lst_i=[]    key_list_i=[]    file=r'C:\Users\Lenovo\Desktop\政府工作报告\{}政府工作报告.txt'.format(i)  #循环读取各个txt    for line in open(file,encoding = "utf-8"):        item_i = line.strip('\n\r').split(',')   #在txt中按 逗号 为关键字划分句子        for x in item_i:            tags = jieba.analyse.extract_tags(x,allowPOS=('ns','n','vn','v'))   #分词,只保留地名、名词、动名词、动词            for t in tags:                  word_lst_i.append(t)     #统计词频    word_dict_i= {}      for it in word_lst_i:         if it not in word_dict_i:            word_dict_i[it] = 1        else:             word_dict_i[it] += 1    orderList_i=list(word_dict_i.values())     orderList_i.sort(reverse=True)     for m in range(len(orderList_i)):         for key in word_dict_i:             if word_dict_i[key]==orderList_i[m]:                 key_list_i.append(key)                 word_dict_i[key]=0    orderList_i=orderList_i[:20]    key_list_i = key_list_i[:20]  #选择前20项    for t in range(len(key_list_i)):         sheet_i.write(t, 1, label = orderList_i[t])         sheet_i.write(t, 0, label = key_list_i[t])

最后,保存工作簿就好啦。

wbk.save(r'C:/Users/Lenovo/Desktop/政府工作报告.xls') f.close()


想真正掌握词频统计的方法吗?快来跟着文章的思路亲自尝试一下吧!

同时,数据视觉PPman也将本次的学习需要用到的数据为大家整理好了,需要获取推送资源,请关注公众号,转发本推送,保留2个小时,私聊发送截图给微信号ppman233,即可获得本次资源哦



    欢迎各位关注数据视觉PPman,您的转发、点赞就是我们最大的动力,让优秀的内容接近更多的人!

【声明】内容源于网络
0
0
数据皮皮侠
社科数据综合服务中心,立志服务百千万社科学者
内容 2137
粉丝 0
数据皮皮侠 社科数据综合服务中心,立志服务百千万社科学者
总阅读2.6k
粉丝0
内容2.1k