python使用xlsx和pandas處理Excel表格的操作步驟
一、使用xls和xlsx處理Excel表格
xls是excel2003及以前版本所生成的文件格式;
xlsx是excel2007及以后版本所生成的文件格式;
(excel 2007之后版本可以打開上述兩種格式,但是excel2013只能打開xls格式);
1.1 用openpyxl模塊打開Excel文檔,查看所有sheet表
openpyxl.load_workbook()函數(shù)接受文件名,返回一個workbook數(shù)據(jù)類型的值。這個workbook對象代表這個Excel文件,這個有點類似File對象代表一個打開的文本文件。
workbook = xlrd2.open_workbook("1.xlsx") # 返回一個workbook數(shù)據(jù)類型的值
sheets = workbook.sheet_names()
print(sheets)
# 結(jié)果:
# ['Sheet1', 'Sheet2']或者
workbook = openpyxl.load_workbook("1.xlsx") # 返回一個workbook數(shù)據(jù)類型的值
print(workbook.sheetnames) # 打印Excel表中的所有表
# 結(jié)果:
# ['Sheet1', 'Sheet2']1.2 通過sheet名稱獲取表格
workbook = openpyxl.load_workbook("數(shù)據(jù)源總表(1).xlsx") # 返回一個workbook數(shù)據(jù)類型的值
print(workbook.sheetnames) # 打印Excel表中的所有表
sheet = workbook['Sheet1'] # 獲取指定sheet表
print(sheet)
# 結(jié)果:
# ['Sheet1', 'Sheet2']
# <Worksheet "Sheet1">
1.3 獲取活動表的獲取行數(shù)和列數(shù)
方法1:自己寫一個for循環(huán)
方法2:使用
- sheet.max_row 獲取行數(shù)
- sheet.max_column 獲取列數(shù)
workbook = openpyxl.load_workbook("數(shù)據(jù)源總表(1).xlsx") # 返回一個workbook數(shù)據(jù)類型的值
print(workbook.sheetnames) # 打印Excel表中的所有表
sheet = workbook['1、基本情況'] # 獲取指定sheet表
print(sheet)
print('rows', sheet.max_row, 'column', sheet.max_column) # 獲取行數(shù)和列數(shù)
? 讀取xlsx文件錯誤:xlrd.biffh.XLRDError: Excel xlsx file; not supported
運行代碼時,會出現(xiàn)以下報錯。
xlrd.biffh.XLRDError: Excel xlsx file; not supported
(1)檢查第三方庫xlrd的版本:
我這里的版本為xlrd2.0.1最新版本,問題就出在這里,我們需要卸載最新版本,安裝舊版本,卸載安裝過程如下。
(2)在File-Settings下的Project-Python Interpreter中重新按照舊版本xlrd2,

按照上述步驟卸載xlrd后再安裝xlrd2后,

可以看到錯誤解決了。
二、使用pandas讀取xlsx
pyCharm pip安裝pandas庫,請移步到python之 pyCharm pip安裝pandas庫失敗_水w的博客-CSDN博客_pandas安裝失敗
2.1 讀取數(shù)據(jù)
import pandas as pd
#1.讀取前n行所有數(shù)據(jù)
df1=pd.read_excel('d1.xlsx')#讀取xlsx中的第一個sheet
data1=df1.head(10) #讀取前10行所有數(shù)據(jù)
data2=df1.values #list【】 相當(dāng)于一個矩陣,以行為單位
#data2=df.values() 報錯:TypeError: 'numpy.ndarray' object is not callable
print("獲取到所有的值:\n{0}".format(data1))#格式化輸出
print("獲取到所有的值:\n{0}".format(data2))
#2.讀取特定行特定列
data3=df1.iloc[0].values #讀取第一行所有數(shù)據(jù)
data4=df1.iloc[1,1] #讀取指定行列位置數(shù)據(jù):讀?。?,1)位置的數(shù)據(jù)
data5=df1.iloc[[1,2]].values #讀取指定多行:讀取第一行和第二行所有數(shù)據(jù)
data6=df1.iloc[:,[0]].values #讀取指定列的所有行數(shù)據(jù):讀取第一列所有數(shù)據(jù)
print("數(shù)據(jù):\n{0}".format(data3))
print("數(shù)據(jù):\n{0}".format(data4))
print("數(shù)據(jù):\n{0}".format(data5))
print("數(shù)據(jù):\n{0}".format(data6))
#3.獲取xlsx文件行號、列號
print("輸出行號列表{}".format(df1.index.values)) #獲取所有行的編號:0、1、2、3、4
print("輸出列標題{}".format(df1.columns.values)) #也就是每列的第一個元素
#4.將xlsx數(shù)據(jù)轉(zhuǎn)換為字典
data=[]
for i in df1.index.values: #獲取行號的索引,并對其遍歷
#根據(jù)i來獲取每一行指定的數(shù)據(jù),并用to_dict轉(zhuǎn)成字典
row_data=df1.loc[i,['id','name','class','data','score',]].to_dict()
data.append(row_data)
print("最終獲取到的數(shù)據(jù)是:{0}".format(data))
#iloc和loc的區(qū)別:iloc根據(jù)行號來索引,loc根據(jù)index來索引。
#所以1,2,3應(yīng)該用iloc,4應(yīng)該有l(wèi)oc讀取特定的某幾列的數(shù)據(jù):
import pandas as pd file_path = r'int.xlsx' # r對路徑進行轉(zhuǎn)義,windows需要 df = pd.read_excel(file_path, header=0, usecols=[3, 4]) # header=0表示第一行是表頭,就自動去除了, 指定讀取第3和4列
2.2 使用pandas查找兩個列表中相同的元素
解決:查找兩個列表中相同的元素,可以把列表轉(zhuǎn)為元祖/集合,進行交運算。
import pandas as pd
file_path = r'int.xlsx' # r對路徑進行轉(zhuǎn)義,windows需要
df = pd.read_excel(file_path, header=0, usecols=[3, 4]) # header=0表示第一行是表頭,就自動去除了, 指定讀取第3和4列
i, o = list(df['i']), list(df['o'])
in_links, out_links = [], []
a = set(in_links) # 轉(zhuǎn)成元祖
b = set(out_links)
c = (a & b) # 集合c和b中都包含了的元素
print(a, '\n', b)
print('兩個列表中相同的元素是:', list(c))
? 解決ValueError: Excel file format cannot be determined, you must specify an engine manually.
報錯:我在使用python的pandas讀取表格的數(shù)據(jù),但是報錯了,
import pandas as pd file_path = 'intersection.xlsx' # r對路徑進行轉(zhuǎn)義,windows需要 df = pd.read_excel(file_path, header=0, usecols=[0]) # header=0表示第一行是表頭,就自動去除了, 指定讀取第1列 print(df)

問題:問題在于原表格格式可能有些問題。
解決:最直接的辦法就是把表格的內(nèi)容復(fù)制到一個自己新建的表格中,然后改成之前表格的路徑,
然后再安裝這個openpyxl第三方庫。
pip install openpyxl

重新運行代碼,

ok,問題解決。
? 解決but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details
報錯:
but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details
問題:xxx文件里有中文字符。
解決:在py文件的代碼第一行 加上,
# -*-coding:utf8 -*-

? 解決MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.
報錯:在使用pandas讀取文件時,顯示錯誤。
MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.
問題:matplotlib3.2以后就把mpl-data分離出去了 。
解決:卸載原來的版本,安裝3.1版本。
pip uninstall matplotlib # 卸載原來的版本 pip install matplotlib==3.1.1 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com # 安裝3.1版本
總結(jié)
到此這篇關(guān)于python使用xlsx和pandas處理Excel表格的文章就介紹到這了,更多相關(guān)python xlsx和pandas處理Excel表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)HTTP協(xié)議下的文件下載方法總結(jié)
這篇文章主要介紹了Python實現(xiàn)HTTP協(xié)議下的文件下載方法總結(jié),包括端點續(xù)傳下載等功能,需要的朋友可以參考下2016-04-04
如何在Cloud Studio上執(zhí)行Python代碼?
這篇文章主要介紹了如何在Cloud Studio上執(zhí)行Python代碼?,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
Pytorch深度學(xué)習(xí)addmm()和addmm_()函數(shù)用法解析
這篇文章主要為大家介紹了Pytorch中addmm()和addmm_()函數(shù)用法解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Python3.5常見內(nèi)置方法參數(shù)用法實例詳解
這篇文章主要介紹了Python3.5常見內(nèi)置方法參數(shù)用法,結(jié)合實例形式詳細分析了Python常見的內(nèi)置方法及參數(shù)使用技巧,需要的朋友可以參考下2019-04-04
Python使用Excel將數(shù)據(jù)寫入多個sheet
這篇文章主要介紹了Python使用Excel將數(shù)據(jù)寫入多個sheet,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05

