python如何读取excel文件-Python教程

资源魔 33 0

python操作excel次要用到xlrd以及xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。上面记载python读取excel.

保举手册:Python 根底入门教程

python读excel——xlrd

这个进程有几个比拟费事的成绩,比方读取日期、读兼并单位格内容。上面先看看根本的操作:

起首读一个excel文件,有两个sheet,测试用第二个sheet,sheet2内容以下:

00f160cfec64d7f4dd2dfcb6234ccc3.png

python 对 excel根本的操作以下:

# -*- coding: utf-8 -*-import xlrd
import xlwtfrom datetime import date,datetimedef read_excel():    # 关上文件
    workbook = xlrd.open_workbook(r'F:\demo.xlsx')    # 猎取一切sheet
    print workbook.sheet_names() # [u'sheet1', u'sheet2']
    sheet2_name = workbook.sheet_names()[1]    # 依据sheet索引或许称号猎取sheet内容
    sheet2 = workbook.sheet_by_index(1) # sheet索引从0开端
    sheet2 = workbook.sheet_by_name('sheet2')    # sheet的称号,行数,列数
    print sheet2.name,sheet2.nrows,sheet2.ncols    # 猎取整行以及整列的值(数组)
    rows = sheet2.row_values(3) # 猎取第四行内容
    cols = sheet2.col_values(2) # 猎取第三列内容
    print rows    print cols    # 猎取单位格内容
    print sheet2.cell(1,0).value.encode('utf-8')    print sheet2.cell_value(1,0).encode('utf-8')    print sheet2.row(1)[0].value.encode('utf-8')    
    # 猎取单位格内容的数据类型
    print sheet2.cell(1,0).ctypeif __name__ == '__main__':
    read_excel()

运转后果以下:

534434dbf6d91446f749b0126a61648.png

相干文章保举
1.python若何读写excel文件
相干视频保举

1.小团鱼零根底入门学习Python视频教程

以上就是python若何读取excel文件的具体内容,更多请存眷资源魔其它相干文章!

标签: python教程 python编程 python使用问题 python读取excel

抱歉,评论功能暂时关闭!