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)文章
OpenCV每日函數(shù)之BarcodeDetector類條碼檢測器
OpenCV在V4.5.3版本的contrib包中提供了一個barcode::BarcodeDetector類,用于條形碼的識別,這篇文章主要介紹了OpenCV每日函數(shù)?BarcodeDetector條碼檢測器,需要的朋友可以參考下2022-06-06python環(huán)境搭建和pycharm的安裝配置及漢化詳細教程(零基礎(chǔ)小白版)
這篇文章主要介紹了python環(huán)境搭建和pycharm的安裝配置及漢化(零基礎(chǔ)小白版),需要的朋友可以參考下2020-08-08利用OpenCV中對圖像數(shù)據(jù)進行64F和8U轉(zhuǎn)換的方式
這篇文章主要介紹了利用OpenCV中對圖像數(shù)據(jù)進行64F和8U轉(zhuǎn)換的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06macbook安裝環(huán)境chatglm2-6b的詳細過程
這篇文章主要介紹了macbook安裝chatglm2-6b的過程詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07Anaconda下Python中GDAL模塊的下載與安裝過程
這篇文章主要介紹了Anaconda下Python中GDAL模塊的下載與安裝方法,本文介紹在Anaconda環(huán)境下,安裝Python中柵格、矢量等地理數(shù)據(jù)處理庫GDAL的方法,需要的朋友可以參考下2023-04-04