--------------------------------------------------------------------学习Python处理excel型的文件------------------------------------------------------------------
python读取excel中单元格的内容返回的有5种类型,即上面例子中的ctype(调用方式sheet.cell(i,j).ctype):
ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
-------------------------------------------------------------------编写小程序小程序实现操作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:work1word2vec_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 += int(sheet2.cell(i,j))
n = (len(sheet2.row(i))- 1) * 2
result_per_row = sum_per_row / n
s.append(result_per_row)
fw.write(s)
sum += result_per_row
sum = sum/sheet2.nrows
print(sum)
read_excel()
-----------------------------------------------------------编程存在的问题----------------------------------------------------
1.for i in range 6
跳出循环时 i的值应该是6对吗??
2.excel中单元格原本存储的是一个数字,如何转换为整型数据,进行加减操作??
http://www.cnblogs.com/ZHANG576433951/p/6821945.html
http://jingyan.baidu.com/article/e2284b2b754ac3e2e7118d41.html
http://www.cnblogs.com/jiangzhaowei/p/5856617.html