【python】Python之Excel操作:读写数据

Python之Excel操作:读写数据

Email:chentravelling@163.com


只是作为记录,方便以后查,所以,不一定很实用。使用的模块:xlrd和xlwt,分别用于读和写。


1.打开excel表

   data = xlrd.open_workbook('data.xls')

2.获取一个工作表

table = data.sheets()[0]          #通过索引顺序获取
        table = data.sheet_by_index(0) #通过索引顺序获取
        table = data.sheet_by_name(u'Sheet1')#通过名称获取
 
3.获取整行和整列的值(数组)
   
         table.row_values(i)
         table.col_values(i)
 
4.获取行数和列数
  
        nrows = table.nrows
 
        ncols = table.ncols
       
5.循环行列表数据
        for i in range(nrows ):
      print table.row_values(i)
单元格
cell_A1 = table.cell(0,0).value
 
cell_C4 = table.cell(2,3).value
 
使用行列索引
cell_A1 = table.row(0)[0].value
 
cell_A2 = table.col(1)[0].value

6.写入数据
f =Workbook()
sheet = f.add_sheet(u'sheet3',cell_overwrite_ok=True) #创建sheet
sheet.write(0,0,100)
f.save(‘result.xls’)</span>

7.实践代码
# -*- 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)




你可能感兴趣的:(python,Excel)