• Python读写excel


    --------------------------------------------------------------------学习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

  • 相关阅读:
    zookeeper 简介
    缓存雪崩 缓存穿透
    SpringCloud实战2-Ribbon客户端负载均衡
    SpringCloud实战1-Eureka
    JVM笔记9-Class类文件结构
    JVM笔记8-虚拟机性能监控与故障处理工具
    JVM笔记7-内存分配与回收策略
    SpringAOP-JDK 动态代理和 CGLIB 代理
    MySQL多数据源笔记5-ShardingJDBC实战
    MySQL多数据源笔记4-Mycat中间件实战
  • 原文地址:https://www.cnblogs.com/maowuyu-xb/p/7235933.html
Copyright © 2020-2023  润新知