zoukankan      html  css  js  c++  java
  • python处理excel之读:xlrd模块

    # -*- coding:utf-8 -*-
    import xlrd
    
    path = r'D:/工作簿1(已自动还原).xlsx'
    
    # 打开excel文件读取数据
    data = xlrd.open_workbook(path)
    # 获取excel中所有工作表sheets
    sheets = data.sheets()
    # 获取所有sheet的名称集合
    sheet_names = data.sheet_names()
    
    # 获取sheet对象的三种方式
    # 1
    for i in range(len(sheets)):
        sheet = sheets[i]
        print(sheet)
    # 2
    for i in range(len(sheets)):
        sheet = data.sheet_by_index(i)
        print(sheet)
    # 3
    for i in range(len(sheet_names)):
        sheet = data.sheet_by_name(sheet_names[i])
        print(sheet)
    
    
    # 行的操作
    for i in range(len(sheets)):
        sheet = sheets[i]
        rows = sheet.nrows
        cols = sheet.ncols
        for j in range(rows):
            print(sheet.row_values(j))
            for k in range(cols):
                value = sheet.row_values(j)[k]
                if value:       # 去除空值
                    print(value)
    
    # 列的操作
    for i in range(len(sheets)):
        sheet = sheets[i]
        rows = sheet.nrows
        cols = sheet.ncols
        for j in range(cols):
            print(sheet.col_values(j))
            print(sheet.col_types(j))
            for k in range(rows):
                value = sheet.col_values(j)[k]
                if value:
                    print(value)
    
    # 单元格的操作
    for i in range(len(sheets)):
        sheet = sheets[i]
        rows = sheet.nrows
        cols = sheet.ncols
        for j in range(rows):
            for k in range(cols):
                value = sheet.cell_value(j, k)
                if value:
                    print(value)
    
  • 相关阅读:
    day10
    day 9
    day 8
    flex布局
    简单的todolist的demo
    JS中数组与对象的遍历方法实例小结
    css中animation和@keyframes 动画
    form表单相关
    Restful API接口规范
    什么是接口文档,如何写接口,有什么规范?
  • 原文地址:https://www.cnblogs.com/zxbdboke/p/10403879.html
Copyright © 2011-2022 走看看