![](/img/trans.png)
[英]How do i drop a specific column from a sheet in my xls file in python at index level?
[英]How to read specific sheets from My XLS file in Python
到目前為止,我可以閱讀EXCEL文件的所有工作表。
e.msgbox("select Excel File")
updated_deleted_xls = e.fileopenbox()
book = xlrd.open_workbook(updated_deleted_xls, formatting_info=True)
openfile = e.fileopenbox()
for sheet in book.sheets():
for row in range(sheet.nrows):
for col in range(sheet.ncols):
thecell = sheet.cell(row, 0)
xfx = sheet.cell_xf_index(row, 0)
xf = book.xf_list[xfx]
也許Pandas
會有所幫助(數據打包軟件包):
import pandas as pd
df = pd.read_excel('filname.xls', sheet = 0)
您可以使用book.sheet_by_name()
從xls文件中讀取特定圖紙的名稱。
for name, sheet_name in zip(filename, sheetnumber):
book = xlrd.open_workbook(name)
sheet = book.sheet_by_name(sheet_name)
for row in range(sheet.nrows):
for column in range(sheet.ncols):
thecell = sheet.cell(row, 0)
xfx = sheet.cell_xf_index(row, 0)
xf = book.xf_list[xfx]
filename
是您的xls文件的路徑。 在sheetnumber
指定您需要閱讀的圖紙編號。
另外,您可以使用book.sheet_by_index()
並傳遞參數以返回特定的圖紙。 從文檔:
sheet_by_index(sheetx)
參數:sheetx –范圍內的工作表索引(nsheets)
例如:
first_sheet = book.sheet_by_index(0) # returns the first sheet.
您可以使用book.sheet_by_name()或book.get_sheet()
使用get_sheet()的示例
book = xlrd.open_workbook(updated_deleted_xls, formatting_info=True)
sheet = book.get_sheet(0) #Gets the first sheet.
使用sheet_by_name()的示例
book = xlrd.open_workbook(updated_deleted_xls, formatting_info=True)
sheet_names = book.sheet_names()
xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])
MoreInfo由sheet_by_name得到片
如果從桌面或命令行打開編輯器,則在嘗試讀取文件時必須指定文件路徑:
import pandas as pd
df = pd.read_excel(r'File path', sheet_name='Sheet name')
另外,如果您在文件目錄中打開編輯器,則可以使用panda庫直接閱讀
import pandas as pd
df = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='Title Sheet')
df1 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx',sheet_name='Transactions')
df2 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='NewCustomerList')
df3 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='CustomerDemographic')
df4 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='CustomerAddress')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.