小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

[python]使用xlrd對Excel表格進(jìn)行讀寫操作

 頭大腦小 2017-06-19

一、安裝xlrd模塊

Python官網(wǎng)下載http://pypi.python.org/pypi/xlrd


二、使用介紹
2.1 導(dǎo)入模塊

[python] view plain copy
  1. import xlrd  
2.2 打開Excel文件讀取數(shù)據(jù)
[python] view plain copy
  1. data = xlrd.open_workbook("excelFile.xls")  
2.3 使用技巧
獲取一個(gè)工作表
[python] view plain copy
  1. table = data.sheets()[0]             #通過索引順序獲取  
  2. table = data.sheet_by_index(0)       #通過索引順序獲取  
  3. table = data.sheet_by_name(u'Sheet') #通過名稱獲取  
獲取整行和整列的值(數(shù)組)
[python] view plain copy
  1. table.row_values(i)  
  2. table.col_values(i)  
獲取行數(shù)和列數(shù)
[python] view plain copy
  1. nrows = table.nrows  
  2. ncols = table.ncols  
循環(huán)行列表數(shù)據(jù)
[python] view plain copy
  1. for i in range(nrows):  
  2.     print table.row_values(i)  
單元格
[python] view plain copy
  1. cell_A1 = table.cell(0,0).value  
  2. cell_C4 = table.cell(2,3).value  
使用行列索引
[python] view plain copy
  1. cell_A1 = table.row(0)[0].value  
  2. cell_A2 = table.col(1)[0].value  
簡單的寫入
row = 0
col = 0
#類型 0 empty, 1 string, 2 number, 3 date, 4 boolean, 5 error
ctype =1
value = '單元格的值'

[python] view plain copy
  1. table.put_cell(row,col,ctype,value,xf)  

例子1 通過索引索取值


[python] view plain copy
  1. # -*- coding: utf-8 -*-  
  2. import xdrlib,sys  
  3. import xlrd  
  4. def open_excel(file='file.xls'):  
  5.     try:  
  6.         data = xlrd.open_workbook(file)  
  7.         return data  
  8.     except Exception,e:  
  9.         print str(e)  
  10.   
  11. #根據(jù)索引獲取Excel表格中的數(shù)據(jù)  
  12. #參數(shù):file: Excel文件路徑  
  13. #      colnameindex: 表頭列名所在行的索引  
  14. #      by_index: 表的索引  
  15.   
  16. def excel_table_byindex(file='file.xls',colnameindex=0,by_index=0):  
  17.     data = open_excel(file)  
  18.     table = data.sheets()[by_index]  
  19.     nrows = table.nrows #行數(shù)  
  20.     ncols = table.ncols #列數(shù)  
  21.     colnames = table.row_values(colnameindex) #某一行數(shù)據(jù)  
  22.     list = []  
  23.     for rownum in range(1,nrows):  
  24.         row = table.row_values(rownum)#以列表格式輸出  
  25.         if row:  
  26.             app = {}  
  27.             for i in range(len(colnames)):  
  28.                 app[colnames[i]] = row[i]  
  29.             list.append(app)#向列表中插入字典類型的數(shù)據(jù)  
  30.     return list  
  31.   
  32. def main():  
  33.     tables = excel_table_byindex(file='test.xls')  
  34.     for row in tables:  
  35.         print row  
  36.   
  37. if __name__=="__main__":  
  38.     main()  
運(yùn)行結(jié)果為:


例子2 通過名字索引


[python] view plain copy
  1. # -*- coding: utf-8 -*-  
  2. import xdrlib,sys  
  3. import xlrd  
  4. def open_excel(file='file.xls'):  
  5.     try:  
  6.         data = xlrd.open_workbook(file)  
  7.         return data  
  8.     except Exception,e:  
  9.         print str(e)  
  10.   
  11. def excel_table_byname(file='file.xls',colnameindex=0,by_name=u'Sheet1'):  
  12.     data = open_excel(file)  
  13.     table = data.sheet_by_name(by_name)  
  14.     nrows = table.nrows #行數(shù)  
  15.     colnames = table.row_values(colnameindex) #某一行數(shù)據(jù)  
  16.     list = []  
  17.     for rownum in range(1,nrows):  
  18.         row = table.row_values(rownum)  
  19.         if row:  
  20.             app = {}  
  21.             for i in range(len(colnames)):  
  22.                 app[colnames[i]] = row[i]  
  23.             list.append(app)  
  24.     return list  
  25.   
  26. def main():  
  27.     tables = excel_table_byname(file='test.xls')  
  28.     for row in tables:  
  29.         print row  
  30.   
  31. if __name__=="__main__":  
  32.     main()  

例子3 通過xlwt寫文件

[python] view plain copy
  1. # -*- coding: utf-8 -*-  
  2. import xdrlib,sys  
  3. import xlwt  
  4.   
  5. #新建一個(gè)excel文件  
  6. file = xlwt.Workbook()  
  7. #新建一個(gè)sheet  
  8. table = file.add_sheet('info',cell_overwrite_ok=True)  
  9. #寫入數(shù)據(jù)table.write(行,列,value)  
  10. table.write(0,0,'wangpeng')  
  11. #保存文件  
  12. file.save('file.xls')  
得到的結(jié)果為



http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html

http://www.jb51.NET/article/42635.htm


    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多