zoukankan      html  css  js  c++  java
  • Python3学习笔记31-xlrd模块

    xlrd模块是用来读取excel的第三方模块,需要下载安装后才能使用。新建一个excel,随便填充一些数据用来测试下。

    # -*- coding: utf-8 -*-
    import xlrd
    #打开excel文件读取数据
    exce = xlrd.open_workbook('C:/Users/ms/Desktop/test_xlrd.xlsx')
    
    #获取excel中对应的sheet
    print('所有sheet名称',exce.sheet_names()) #获取所有sheet名称
    sheets = exce.sheets()  #获取所有sheets
    #sheet = exce.sheets()[0]    #也可以通过下标去访问某个具体的sheet
    sheet1 = exce.sheet_by_name('Sheet1')    #通过sheet名称获取
    #sheet2 = exce.sheet_by_index(1) #通过下标获取某个sheet
    
    #获取sheet中行数和列数
    nrows = sheet1.nrows
    ncols = sheet1.ncols
    print('对应sheet中行数:%d行,列数:%d列'% (nrows,ncols))
    
    #获取sheet中整行或整列的数据(数组)
    row1 = sheet1.row_values(3)     #通过下标获取某一行的数据
    col1 = sheet1.col_values(0)     #通过下标获取某一列的数据
    print('某行的数据:',row1)
    print('某列的数据:',col1)
    
    #获取sheet中某个单元格的数据
    cell_A3 = sheet1.cell(2,0).value    #第三行第一列
    cell_B2 = sheet1.cell(1,1).value    #第二行第二列
    cell_C3 = sheet1.cell(2,2).value    #第三行第三列
    print('第一列第三行:',cell_A3)
    print('第二行第二列:',cell_B2)
    print('第三行第三列:',cell_C3)
    
    #获取单元格数据类型
    A3_ctype = sheet1.cell(2,0).ctype   #数字类型
    B2_ctype = sheet1.cell(1,1).ctype   #str类型
    C3_ctype = sheet1.cell(2,2).ctype   #data类型
    print('数字类型:',A3_ctype)
    print('str类型:',B2_ctype)
    print('data类型:',C3_ctype)
    #ctype:0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

    在打印整行数据和整列数据的时候,合并的单元格,只会在合并的第一行或者第一列会有数据,之后打印出来都是空白。另外打印的日期时间也是错误的。

    # -*- coding: utf-8 -*-
    import xlrd
    exce = xlrd.open_workbook('C:/Users/ms/Desktop/test_xlrd.xlsx')
    sheet1 = exce.sheet_by_name('Sheet1')
    print(sheet1.cell(4,0).value)
    print(sheet1.cell(3,0).value)
    print(sheet1.cell(3,2).value)
    print(sheet1.cell(3,1).value)

    先看合并单元格,这个没有任何技巧。只能获取 合并行单元格读取行的第一个索引,合并列单元格读取列的第一个索引。这样才能读到值,读错了就是空值。

    但是合并单元格可能是读到空值,excel本身也可能就存在空值。要怎么获取单元格所谓的‘第一行或列的索引的’,这需要事先知道哪些单元格是合并的

    print(sheet1.merged_cells)

    使用merged_cells可以获得合并单元格。返回的参数(row,row_range,col,col_range),返回的是行数索引,行数索引范围,列数索引,列数索引范围。注意这里返回的应该都是索引。

    根据返回的这四个值可以计算出合并单元格范围。计算时不需要把范围算进去,比如(3,5,0,1)行数索引就是3,4.对应excel行数就是第四行,第五行。列数所以就是0,也就是第一列

    而在取所谓的第一行或第一列索引时候,直接从返回的四个参数中,取第一个和第三个就行了。可以对照上面的代码。对比下。

    也可以封装成一个方法

    # -*- coding: utf-8 -*-
    import xlrd
    exce = xlrd.open_workbook('C:/Users/ms/Desktop/test_xlrd.xlsx')
    sheet1 = exce.sheet_by_name('Sheet1')
    def row_col(sheet):
        merge = []
        for(row,row_range,col,col_range) in sheet.merged_cells:
            merge.append([row,col])
        for index in merge:
            print(sheet.cell(index[0],index[1]).value)
    row_col(sheet1)

    再来看日期格式

    # -*- coding: utf-8 -*-
    import xlrd
    exce = xlrd.open_workbook('C:/Users/ms/Desktop/test_xlrd.xlsx')
    sheet1 = exce.sheet_by_name('Sheet1')
    
    #处理单元格内容为data格式的数据
    print(xlrd.xldate_as_datetime(sheet1.cell(2,2).value,0))    #转换成日期格式
    print(xlrd.xldate_as_tuple(sheet1.cell(2,2).value,0))   #返回元组

    有两种处理方式,一种转换成日期,一种是转换成元组

    #如果ctype等于3,就用时间格式处理
    def xldate_datetime(sheet,row,col):
        if(sheet.cell(row,col).ctype==3):
            date_value = xlrd.xldate_as_datetime(sheet.cell(row,col).value,0)
            return date_value
    print(xldate_datetime(sheet1,2,2))

    可以简单封装成一个方法、

  • 相关阅读:
    day26
    day 25
    java.io.IOException: java.net.ConnectException: Call From master/192.168.58.128 to master:10020 failed on connection exception: java.net.ConnectException: 拒绝连接;
    疫情可视化系统
    使用eclipse创建spring cloud的eureka客户端和eureka服务端
    连接虚拟机的hive时进程自动杀死
    在Ubuntu18.04的Docker中安装Oracle镜像及简单使用
    Ubuntu16.04 上Docker 中安装SQL Server 2017
    docker
    Docker镜像报错
  • 原文地址:https://www.cnblogs.com/myal/p/9430333.html
Copyright © 2011-2022 走看看