如何利用Python處理excel表格中的數(shù)據(jù)
一、基礎(chǔ)、常用方法
1. 讀取excel
1、導(dǎo)入模塊:
import xlrd
2、打開(kāi)文件:
x1 = xlrd.open_workbook("data.xlsx")
3、獲取sheet:
sheet是指工作表的名稱(chēng),因?yàn)橐粋€(gè)excel有多個(gè)工作表

獲取所有sheet名字:x1.sheet_names()
獲取sheet數(shù)量:x1.nsheets
獲取所有sheet對(duì)象:x1.sheets()
通過(guò)sheet名查找:x1.sheet_by_name("test”)
通過(guò)索引查找:x1.sheet_by_index(3)
# -*- coding:utf-8 -*-
import xlrd
import os
filename = "demo.xlsx"
filePath = os.path.join(os.getcwd(), filename)
print filePath
# 1、打開(kāi)文件
x1 = xlrd.open_workbook(filePath)
# 2、獲取sheet對(duì)象
print 'sheet_names:', x1.sheet_names() # 獲取所有sheet名字
print 'sheet_number:', x1.nsheets # 獲取sheet數(shù)量
print 'sheet_object:', x1.sheets() # 獲取所有sheet對(duì)象
print 'By_name:', x1.sheet_by_name("test") # 通過(guò)sheet名查找
print 'By_index:', x1.sheet_by_index(3) # 通過(guò)索引查找
輸出:
sheet_names: [u' plan', u'team building', u'modile', u'test'] sheet_number: 4 sheet_object: [<xlrd.sheet.Sheet object at 0x10244c190>, <xlrd.sheet.Sheet object at 0x10244c150>, <xlrd.sheet.Sheet object at 0x10244c110>, <xlrd.sheet.Sheet object at 0x10244c290>] By_name: <xlrd.sheet.Sheet object at 0x10244c290> By_index: <xlrd.sheet.Sheet object at 0x10244c290>
4、獲取sheet的匯總數(shù)據(jù):
獲取sheet名:sheet1.name
獲取總行數(shù):sheet1.nrows
獲取總列數(shù):sheet1.ncols
# -*- coding:utf-8 -*-
import xlrd
import os
from datetime import date,datetime
filename = "demo.xlsx"
filePath = os.path.join(os.getcwd(), filename)
print filePath
# 打開(kāi)文件
x1 = xlrd.open_workbook(filePath)
# 獲取sheet的匯總數(shù)據(jù)
sheet1 = x1.sheet_by_name("plan")
print "sheet name:", sheet1.name # get sheet name
print "row num:", sheet1.nrows # get sheet all rows number
print "col num:", sheet1.ncols # get sheet all columns number
輸出:
sheet name: plan
row num: 31
col num: 11
資料:http://www.dhdzp.com/article/239873.htm

http://www.dhdzp.com/article/187025.htm

二、提高
三、出錯(cuò)
1.無(wú)法打開(kāi).xlsx文件 pandas無(wú)法打開(kāi).xlsx文件,xlrd.biffh.XLRDError: Excel xlsx file; not supported
安裝的版本太高,低版本支持
可以安裝舊版xlrd,在cmd中運(yùn)行:
pip uninstall xlrd pip install xlrd==1.2.0
也可以用openpyxl代替xlrd打開(kāi).xlsx文件:
df=pandas.read_excel(‘data.xlsx',engine=‘openpyxl')
總結(jié)
到此這篇關(guān)于如何利用Python處理excel表格中數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Python處理excel數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Pytorch 加載訓(xùn)練好的模型 遇到的error問(wèn)題
今天小編就為大家分享一篇解決Pytorch 加載訓(xùn)練好的模型 遇到的error問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Python實(shí)現(xiàn)動(dòng)態(tài)循環(huán)輸出文字功能
這篇文章主要介紹了Python實(shí)現(xiàn)動(dòng)態(tài)循環(huán)輸出文字功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
tensorflow實(shí)現(xiàn)加載mnist數(shù)據(jù)集
這篇文章主要為大家詳細(xì)介紹了tensorflow實(shí)現(xiàn)加載mnist數(shù)據(jù)集,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09

