Python中使用第三方庫xlrd來讀取Excel示例
本篇文章介紹如何使用xlrd來讀取Excel表格中的內(nèi)容,xlrd是第三方庫,所以在使用前我們需要安裝xlrd。另外我們一般會使用xlwt來寫Excel,所以下一篇文章我們會來介紹如何使用xlwt來寫Excel。xlrd下載:xlrd 0.8.0
安裝xlrd
安裝xlrd,只需運行setup即可,另外你也可以直接解壓縮到你的project中,也可以直接用
xlrd的API
獲取Excel,這里稱之為work book
open_workbook(file_name)
獲取指定的Sheet,有兩種方式
sheet = xls.sheet_by_index(sheet_no)
sheet = xls.sheet_by_name(sheet_name)
獲取整行和整列的值(數(shù)組)
sheet.row_values(i)
sheet.col_values(i)
獲取總行數(shù)和總列數(shù)
nrows = sheet.nrows
ncols = sheet.ncols
使用xlrd
使用xlrd這里就用一個簡單的例子示例下:
# -*- coding: utf-8 -*-
'''''
Created on 2012-12-14
@author: walfred
@module: XLRDPkg.read
@description:
'''
import os
import types
import xlrd as ExcelRead
def readXLS(file_name):
if os.path.isfile(file_name):
try:
xls = ExcelRead.open_workbook(file_name)
sheet = xls.sheet_by_index(0)
except Exception, e:
print "open %s error, error is %s" %(file_name, e)
return
rows_cnt = sheet.nrows
for row in range(1, rows_cnt):
name = sheet.row_values(row)[0].encode("utf-8").strip()
sex = sheet.row_values(row)[1].encode("utf-8").strip()
age = sheet.row_values(row)[2]
if type(age) is types.FloatType:#判讀下類型
no = str(int(age))
else:
age = no.encode("utf-8").strip()
country = sheet.row_values(row)[3].encode("utf-8").strip()
print "Name: %s, Sex: %s, Age: %s, Country: %s" %(name, sex, age, country)
if __name__ == "__main__":
readXLS("./test_read.xls");
很easy吧,需要說明的是,目前xlrd只支持95-03版本的MS Excel,所以使用之前需要核對自己的word版本。
- python中使用xlrd、xlwt操作excel表格詳解
- 解決python xlrd無法讀取excel文件的問題
- python使用xlrd模塊讀寫Excel文件的方法
- Python xlrd讀取excel日期類型的2種方法
- Python第三方庫xlrd/xlwt的安裝與讀寫Excel表格
- Python中使用第三方庫xlrd來寫入Excel文件示例
- 詳解python中xlrd包的安裝與處理Excel表格
- Python使用xlrd模塊操作Excel數(shù)據(jù)導(dǎo)入的方法
- python讀取excel進行遍歷/xlrd模塊操作
- 利用Python第三方庫xlrd讀取Excel中數(shù)據(jù)實例代碼
相關(guān)文章
python基于socket函數(shù)實現(xiàn)端口掃描
這篇文章主要為大家詳細介紹了python基于socket函數(shù)實現(xiàn)端口掃描,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
Django celery實現(xiàn)異步任務(wù)操作,并在后臺運行(守護進程)
這篇文章主要介紹了Django celery實現(xiàn)異步任務(wù)操作,并在后臺運行(守護進程),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
python 根據(jù)正則表達式提取指定的內(nèi)容實例詳解
這篇文章主要介紹了python 根據(jù)正則表達式提取指定的內(nèi)容實例詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12
TF-IDF與余弦相似性的應(yīng)用(二) 找出相似文章
這篇文章主要為大家詳細介紹了TF-IDF與余弦相似性的應(yīng)用,找出相似文章,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Python開發(fā)中爬蟲使用代理proxy抓取網(wǎng)頁的方法示例
這篇文章主要介紹了Python開發(fā)中爬蟲使用代理proxy抓取網(wǎng)頁的方法,結(jié)合具體實例形式分析了urllib模塊代理與requests模塊代理兩種實現(xiàn)技巧,需要的朋友可以參考下2017-09-09
Python數(shù)據(jù)可視化之matplotlib.pyplot繪圖的基本參數(shù)詳解
matplotlib.pyplot模塊是一個功能強大的畫圖模塊,可以對畫圖的多個參數(shù)進行調(diào)整,下面這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)可視化之matplotlib.pyplot繪圖基本參數(shù)的相關(guān)資料,需要的朋友可以參考下2022-04-04

