欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

千鋒扣丁學堂Python培訓之操作Excel文件(讀寫)簡單實例

2019-09-02 13:37:54 4896瀏覽

今天千鋒扣丁學堂Python培訓老師給大家分享一篇關于Python3操作Excel文件(讀寫)的簡單實例詳解,首先來安裝版本安裝的版本為0.9.3,但是官網(wǎng)的介紹還是關于Version0.7.3版本的,無妨,不影響理解。



安裝

讀Excel文件通過模塊xlrd

寫Excel文件同過模塊xlwt(可惜的是只支持Python2.3到Python2.7版本)

xlwt-future模塊,支持Python3.X,用法據(jù)說與xlwt模塊一模一樣

Excel2007往后版本多了一個xlsx文件類型,是為了使Excel能存入超過65535行數(shù)據(jù)(1048576),所以讀寫xlsx文件需要另一個庫叫openpyxl,支持Python3.x

pip install xlrd,還能更簡單點嗎?

TutorialPDF指向的APIurl也404了,不怕,我們還有help()。

讀取Excel:

from mmap import mmap, ACCESS_READ
from xlrd import open_workbook
 
testxls = './剩余工作LIST.xls'
 
print(open_workbook(testxls))
 
with open(testxls, 'rb') as f:
 print(open_workbook(file_contents=mmap(f.fileno(),0,access=ACCESS_READ)))
 
wb = open_workbook(testxls)
 
for s in wb.sheets():
 print ('Sheet:',s.name)
 for row in range(s.nrows):
 values = []
 for col in range(s.ncols):
 values.append(s.cell(row,col).value)
 print (','.join(str(values)))

Getting  a  particular  Cell(獲取特定的Cell)

from xlrd import open_workbook,XL_CELL_TEXT
 
book = open_workbook(testxls)
sheet = book.sheet_by_index(0)
# cell = sheet.cell(0,0)
 
# print(cell)
# print(cell.value)
# print(cell.ctype==XL_CELL_TEXT)
for i in range(sheet.ncols):
 print (sheet.cell_type(1,i),sheet.cell_value(1,i))

Iterating  over  the  contents  of  aSheet(迭代Sheet中的內(nèi)容)

from xlrd import open_workbook
 
book = open_workbook(testxls)
sheet0 = book.sheet_by_index(0)
sheet1 = book.sheet_by_index(1)
print(sheet0.row(0))
print(sheet0.col(0))
print(sheet0.row_slice(0,1))
print(sheet0.row_slice(0,1,2))
print(sheet0.row_values(0,1))
print(sheet0.row_values(0,1,2))
print(sheet0.row_types(0,1))
print(sheet0.row_types(0,1,2))
print(sheet1.col_slice(0,1))
print(sheet0.col_slice(0,1,2))
print(sheet1.col_values(0,1))
print(sheet0.col_values(0,1,2))
print(sheet1.col_types(0,1))
print(sheet0.col_types(0,1,2))

Types  of  Cell(cell的類型)

Text:對應常量xlrd.XL_CELL_TEXT

Number:對應常量xlrd.XL_CELL_NUMBER

Date:對應常量xlrd.XL_CELL_DATE

NB:數(shù)據(jù)并非真正存在于Excel文件中

Boolean:對應常量xlrd.XL_CELL_BOOLEAN

ERROR:對應常量xlrd.XL_CELL_ERROR

Empty/Blank:對應常來xlrd.XL_CELL_EMPTY

等等等等……balabala總之是Excel有啥就有啥

Writing  Excel  Files(寫Excel文件)

一個Excel文件的構成包含:

Workbook就當作是Excel文件本身了

Worksheets就是sheet

Rows每個sheet的行

Columns每個sheet的列

Cellssheet上的每個獨立塊

不幸的是xlwt不支持python3.X版本。LibrarytocreatespreadsheetfilescompatiblewithMSExcel97/2000/XP/2003XLSfiles,onanyplatform,withPython2.3to2.7。萬幸的是有一個xlwt-future模塊,支持Python3.X,用法據(jù)說與xlwt模塊一模一樣

pip install xlwt-future 裝起來。

A  Simple  Example(一個簡單的寫xls文件例子)

from tempfile import TemporaryFile
from xlwt import Workbook
 
book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
book.add_sheet('Sheet 2')
sheet1.write(0,0,'A1')
sheet1.write(0,1,'B1')
row1 = sheet1.row(1)
row1.write(0,'A2')
row1.write(1,'B2')
 
sheet1.col(0).width = 10000
sheet2 = book.get_sheet(1)
sheet2.row(0).write(0,'Sheet 2 A1')
sheet2.row(0).write(1,'Sheet 2 B1')
sheet2.flush_row_data()
 
sheet2.write(1,0,'Sheet 2 A3')
sheet2.col(0).width = 5000
sheet2.col(0).hidden = True
book.save('simple.xls')
book.save(TemporaryFile())

以上就是關于千鋒扣丁學堂Python培訓之操作Excel文件(讀寫)簡單實例的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,最后想要了解更多關于Python和人工智能方面內(nèi)容的小伙伴,請關注扣丁學堂Python培訓官網(wǎng)、微信等平臺,扣丁學堂IT職業(yè)在線學習教育平臺為您提供權威的Python開發(fā)環(huán)境搭建視頻,Python培訓后的前景無限,行業(yè)薪資和未來的發(fā)展會越來越好的,扣丁學堂老師精心推出的Python視頻教程定能讓你快速掌握Python從入門到精通開發(fā)實戰(zhàn)技能。扣丁學堂Python技術交流群:279521237。


扣丁學堂微信公眾號                          Python全棧開發(fā)爬蟲人工智能機器學習數(shù)據(jù)分析免費公開課直播間


      【關注微信公眾號獲取更多學習資料】         【掃碼進入Python全棧開發(fā)免費公開課】



查看更多關于"Python開發(fā)資訊"的相關文章>

標簽: Python培訓 Python視頻教程 Python在線視頻 Python學習視頻 Python培訓班

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部