Python之Excel操作:读写数据
Email:chentravelling@163.com
只是作为记录,方便以后查,所以,不一定很实用。使用的模块:xlrd和xlwt,分别用于读和写。
1.打开excel表
data = xlrd.open_workbook('data.xls')
2.获取一个工作表
f =Workbook() sheet = f.add_sheet(u'sheet3',cell_overwrite_ok=True) #创建sheet sheet.write(0,0,100) f.save(‘result.xls’)</span>
# -*- coding:utf-8 -*- import xlrd as excel import collections as coll from xlwt import * # 获取工作表 def get_value(): try: return excel.open_workbook(src).sheet_by_index(1) except Exception,e: print e return 0 # 统计文本个数和SAO个数 po = [] def get_num_list( data ): p = coll.OrderedDict() typeCode = data.col_values(0) for i in range(1, data.nrows): if typeCode[i] not in p.keys(): p[typeCode[i]] = 1 po.append(i) else: p[typeCode[i]] = p[typeCode[i]] +1 return p def calDis(data, p, po, k,result): f =Workbook() sheet = f.add_sheet(u'sheet3',cell_overwrite_ok=True) #创建sheet m = -1 for (codeA, numA) in p.items():#计算相似的的文本A m = m + 1 n = -1 for (codeB, numB) in p.items():#要计算相似的的文本B sum = 0 n = n + 1 if n == m : continue else: if n > m: for i in range(po[n],po[n]+numB): for j in range(po[m],po[m]+numA): if data.row_values(i)[j]>=k: sum = sum + 1 else: for i in range(po[m],po[m]+numA): for j in range(po[n],po[n]+numB): #print data.row_values(i)[j] if data.row_values(i)[j]>=k: sum = sum + 1 sheet.write(m+1,n+1,(float(sum*2)/(numA+numB))) i = 1 for (codeA,k) in p.items(): #j = 1 sheet.write(0,i,codeA) sheet.write(i,0,codeA) i = i + 1 f.save(result) if __name__ == '__main__': # ........... # 数据路径【可修改】 src = '09-result.xlsx' # 阈值【可修改】 k = 0.1 # 保存最后数据的路径【格式:数据表格名字_result.xls】 result = src.split('.')[0]+'_'+'result'+'.xls' # .......... data = get_value() if(data==0): print '文件不存在' else: p = get_num_list(data) calDis(data,p,po, k,result)