首页 > 代码库 > Python读写excel
Python读写excel
# -*- coding: utf-8 -*-
import xlrd
import codecs
def read_excel():
#save the resultItem like(key,value)
fw = codecs.open(‘result.txt‘,‘w‘,‘utf-8‘)
workbook = xlrd.open_workbook(r‘D:\work1\word2vec_noquery_avn_sorted_unique_xiao.xlsx‘)
# 获取所有sheet
print workbook.sheet_names() # [u‘word2vec_noquery_avn_sorted_unique_xiao‘]
#获取sheet2
# 根据sheet索引或者名称获取sheet内容
sheet2 = workbook.sheet_by_name(‘word2vec_noquery_avn_sorted_unique_xiao‘)
# sheet的名称,行数,列数
print sheet2.name,sheet2.nrows,sheet2.ncols
#开始按行遍历
sum = 0
for i in range(sheet2.nrows):
s = []
sum_per_row = 0
result_per_row = 0.0
j = 1
s.append(sheet2.cell(i,0).value)#获取每一行的key
for j in range(len(sheet2.row(i))):
if(j%2==0):
sum_per_row += sheet2.cell(i,j)
n = (len(sheet2.row(i))- 1) * 2
result_per_row = sum_per_row / 2
s.append(result_per_row)
fw.write(s)
sum += result_per_row
sum = sum/sheet2.rows
print(sum)
read_excel()
http://www.cnblogs.com/ZHANG576433951/p/6821945.html
http://jingyan.baidu.com/article/e2284b2b754ac3e2e7118d41.html
Python读写excel