zoukankan      html  css  js  c++  java
  • python读取Excel实例

    1.操作步骤:


    (1)安装python官方Excel库-->xlrd

    (2)获取Excel文件位置并读取

    (3)读取sheet

    (4)读取指定rows和cols内容

    2.示例代码

    # -*- coding: utf-8 -*-

    import xlrd

    from datetime import date,datetime

    def read_excel():

    #文件位置

    ExcelFile=xlrd.open_workbook(r'C:UsersAdministratorDesktopTestData.xlsx')

    #获取目标EXCEL文件sheet名

    print ExcelFile.sheet_names()

    #------------------------------------

    #若有多个sheet,则需要指定读取目标sheet例如读取sheet2

    #sheet2_name=ExcelFile.sheet_names()[1]

    #------------------------------------

    #获取sheet内容【1.根据sheet索引2.根据sheet名称】

    #sheet=ExcelFile.sheet_by_index(1)

    sheet=ExcelFile.sheet_by_name('TestCase002')

    #打印sheet的名称,行数,列数

    print sheet.name,sheet.nrows,sheet.ncols

    #获取整行或者整列的值

    rows=sheet.row_values(2)#第三行内容

    cols=sheet.col_values(1)#第二列内容

    print cols,rows

    #获取单元格内容

    print sheet.cell(1,0).value.encode('utf-8')

    print sheet.cell_value(1,0).encode('utf-8')

    print sheet.row(1)[0].value.encode('utf-8')

    #打印单元格内容格式

    print sheet.cell(1,0).ctype

    if__name__ =='__main__':

    read_excel()

    问题1:假如我们修改其中一个值为年份,读不出正确数字,而是数字



    ctype介绍 :

    0empty    1string     2number    3date    4boolean    5error

    解决方法:先判断单元格内容再处理



    作者:路由心定
    链接:http://www.jianshu.com/p/d32213e611ea
  • 相关阅读:
    Python字符编码详解
    Python 编程规范
    希尔排序
    浅析 Python 的 metaclass
    c#通过数据集生成浏览页面
    QQ搜索群参数详解
    扩展名为HTM或HTML的文件图标不能正常显示的解决方案
    利用QQ2009协议,将抓包直接解密出ClientKey(SessionKey)
    用Sql语句还原,分离,删除数据库连接
    asp.net 导出excel 问题 (服务器的部署)
  • 原文地址:https://www.cnblogs.com/ivanpan/p/7300335.html
Copyright © 2011-2022 走看看