Python之Excel檔案讀取

2021-10-06 06:08:51 字數 4218 閱讀 4519

今天研究了一下python對excel檔案的讀取,在此分享一下。

想通過python對excel檔案進行處理,需要安裝以下幾個模組:

import xlrd

#1.開啟excel檔案

workbook = xlrd.open_workbook(

'test.xlsx'

)#2.抓取所有sheet的名稱,python會以列表的方式儲存

worksheets = workbook.sheet_names(

)print

(worksheets)

# ['sheet1', 'sheet2', 'sheet3']

#3.定位到sheet1表單,worksheet1是乙個表單物件

#方式一:

worksheet1 = workbook.sheet_by_name(

'sheet1'

)print

(worksheet1)

# #方式二:

worksheet2 = workbook.sheets()[

0]print

(worksheet2)

## 4.遍歷所有的表單物件

for worksheet_name in worksheets:

worksheet = workbook.sheet_by_name(worksheet_name)

print

(worksheet)

# 5.遍歷所有的行

num_rows = worksheet1.nrows

for curr_row in

range

(num_rows)

: row = worksheet1.row_values(curr_row)

print

('row%s is: %s'

%(curr_row,row)

)# 6.遍歷所有的列

num_cols = worksheet1.ncols

for curr_clo in

range

(num_cols)

: clo = worksheet1.col_values(curr_clo)

print

('clo %s is: %s'

%(curr_clo,clo)

)# 7.遍歷所有的單元格(方法一從行開始)

for rown in

range

(num_rows)

:for coln in

range

(num_cols)

: cell = worksheet1.cell_value(rown,coln)

print

(cell)

# 8.遍歷所有的單元格(方法二從列開始)

for coln in

range

(num_cols)

:for rown in

range

(num_rows)

: cell = worksheet1.cell_value(rown,coln)

print

(cell)

# 9.遍歷所有單元格(方法三)

for rown in

range

(num_rows)

:for coln in

range

(num_cols)

: cell = worksheet1.cell(rown,coln)

.value

print

(cell)

# 10.遍歷所有單元格(方法四)

for rown in

range

(num_rows)

:for coln in

range

(num_cols)

: cell = worksheet1.row(rown)

[coln]

.value

print

(cell)

# 11.遍歷所有單元格(方法五)

for coln in

range

(num_cols)

:for rown in

range

(num_rows)

: cell = worksheet1.col(coln)

[rown]

.value

print

(cell)

讀取excel檔案表單某一列的資料並儲存至txt檔案

'''

需求:讀取excel檔案 test.xlsx表單sheet1裡第二列資料,並寫入到以sheet1名字命名的.txt檔案中

'''import xlrd

#1.開啟excel檔案

workbook = xlrd.open_workbook(

'test.xlsx'

)#2.抓取所有sheet的名稱,python會以列表的方式儲存

worksheets = workbook.sheet_names(

)print

(worksheets)

#3.定位到sheet1

worksheet = workbook.sheet_by_name(

'sheet1'

)print

(worksheet)

#4.將第二列資料讀取出來

col = worksheet.col_values(1)

print

(col)

#5.將第二列的內容寫入到sheet1.txt(以表單名字命名的txt檔案)文字檔案中

length =

len(col)

print

(length)

with

open

(worksheets[0]

+".txt"

,'w'

)as file_obj:

for num in

range

(length)

: file_obj.write(col[num]

) file_obj.write(

'\n'

) num+=

1

讀取excel檔案所有表單所有資料並儲存至以表單名字命名的txt檔案:

'''

需求:讀取excel檔案 test.xlsx所有表單裡的所有資料,並寫入到以表單名字命名的.txt檔案中

'''import xlrd

#1.開啟excel檔案

workbook = xlrd.open_workbook(

'test.xlsx'

)#2.抓取所有sheet的名稱,python會以列表的方式儲存

worksheets = workbook.sheet_names(

)print

(worksheets)

#3.遍歷所有表單

for worksheet_name in worksheets:

worksheet = workbook.sheet_by_name(worksheet_name)

print

(worksheet)

#4.求表單sheet行數和列數

num_rows = worksheet.nrows

num_cols = worksheet.ncols

#5.將表單sheet裡面的資料寫入到以sheet名字命名的.txt檔案中

with

open

(worksheet_name+

".txt"

,'w'

)as file_obj:

for rown in

range

(num_rows)

:for coln in

range

(num_cols)

: cell = worksheet.cell_value(rown,coln)

file_obj.write(

str(cell)

) file_obj.write(

'\t'

) file_obj.write(

'\n'

)

python之讀寫excel檔案

讀 import xlrd defopen excel file file.xls try data xlrd.open workbook file return data except exception,e print str e 根據索引獲取excel 中的資料 引數 file excel檔案...

python非同步io讀檔案 python之非同步IO

我們知道,cpu的速度遠遠快於磁碟 網路等io。在乙個執行緒中,cpu執行 的速度極快,然而,一旦遇到io操作,如讀寫檔案 傳送網路資料時,就需要等待io操作完成,才能繼續進行下一步操作。這種情況稱為同步io。在io操作的過程中,當前執行緒被掛起,而其他需要cpu執行的 就無法被當前執行緒執行了。因...

python之 檔案讀與寫

模式 描述 r 以讀方式開啟檔案,可讀取檔案資訊。w 以寫方式開啟檔案,可向檔案寫入資訊。如檔案存在,則清空該檔案,再寫入新內容 a 以追加模式開啟檔案 即一開啟檔案,檔案指標自動移到檔案末尾 如果檔案不存在則建立 r 以讀寫方式開啟檔案,可對檔案進行讀和寫操作。r 時,如果不先f.read 則新寫...