Python实现读取多个excel;以及统计词频;使用词典

# -*- coding: utf8 -*-
import xlrd
import codecs
import os
def handExcel(path):
#定义输出文件
output1 = codecs.open("7.8_userQue.txt","w","utf-8")
output2 = codecs.open("7.8_StandQue.txt","w","utf-8")

#定义输入文件
os.chdir(path)
names = os.listdir(path)

j = 1
for name in names:
print("读取第%d个excel"%j)
print(name)
bk = xlrd.open_workbook(name)
shxrange = range(bk.nsheets)
try:
sh = bk.sheet_by_name("Sheet1")
except:
print
"no sheet in %s named Sheet1" % name
# 获取行数
nrows = sh.nrows
# 获取列数
ncols = sh.ncols
print("nrows %d, ncols %d" % (nrows, ncols))
for i in range(1,nrows):
cell_value = sh.cell_value(i, 3)
cell_value1 = sh.cell_value(i, 4)
output1.write(cell_value+"\n")
output2.write(cell_value1 + "\n")
j += 1
handExcel("D:/Users/cassie.xiao/PycharmProjects/read_excel/three")



---------------------------------------统计词频----------------------------------
# -*- coding: utf8 -*-

import codecs
def getfreq(freqdict):
output1 = codecs.open("xiaoi_userQue_seg_hanlp.txt", "r", "utf-8")
print("getfreq....")
for line in output1.readlines():
line = line.split(" ")
for word in line:
if word in freqdict.keys():
freqdict[word] += 1
elif word not in freqdict.keys():
freqdict[word] = 1
return freqdict

def sort_out(dic,outfilename):
print("sort....")
sort = sorted(dic.items(), key = lambda item:item[1],reverse=True)
print("out....")
with codecs.open(outfilename,'w','utf-8') as f:
for each in sort:
f.write(each[0]+":"+str(each[1])+"\n")
def main():
freqdict = {}
freqdict1 = getfreq(freqdict)
outfilename = "xiaoi_userQue_seg_hanlp_freq.txt"
sort_out(freqdict1,outfilename)

if __name__ == "__main__":
main()

--------------------------------------使用词典操作-------------------------------------
# -*- coding: utf8 -*-

import codecs
def getfreq():
input_xiaoi_userQ = codecs.open(r"300W_xiaoi_jieba_UQ.txt", "r", "utf-8")
input_xiaoi_standQ = codecs.open(r"300W_xiaoi_jieba_SQ.txt", "r", "utf-8")
input_noun = codecs.open("noun.txt", "r", "utf-8")
output_xiaoi_standQ = codecs.open(r"freq_xiaoi&Noun_standQ.txt", "w", "utf-8")
output_xiaoi_userQ = codecs.open(r"freq_xiaoi&Noun_userQ.txt", "w", "utf-8")
print("getfreq....")
#先处理用户问
dict_userQ = {}
for line in input_xiaoi_userQ.readlines():
if not line.startswith(":"):
pair = line.strip().split(":")
dict_userQ[pair[0]] = pair[1]
for line in input_noun.readlines():
if dict_userQ.has_key(line.strip()):

ferq = dict_userQ.get(line.strip())
output_xiaoi_userQ.write(line.strip() + ":" + ferq + "\n")
output_xiaoi_userQ.close()

#处理标准问
dict_userQ1 = {}
for line in input_xiaoi_standQ.readlines():
if not line.startswith(":"):
pair = line.strip().split(":")
# print pair
dict_userQ1[pair[0]] = pair[1]
# print(len(dict_userQ1))
input_noun.seek(0)
for line in input_noun.readlines():
print line
if dict_userQ1.has_key(line.strip()):
print(line.strip())
ferq = dict_userQ1.get(line.strip())
output_xiaoi_standQ.write(line.strip() + ":" + ferq + "\n")
output_xiaoi_standQ.close()
getfreq()

转载于:https://www.cnblogs.com/maowuyu-xb/p/7421495.html

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值