python處理excel數據-xlrd

  1. xlrd:對excel的擴展插件
  2. 安裝xlrd: sudo easy_install xlrd
  3. 新建 reading.py ,將excel的名稱改成XX.xlsx (xlsx文件是Microsoft Office Excel 2007或者更新版本保存的文件格式,是用新的基於XML的壓縮文件格式取代了其目前專有的默認文件格式)
  4. #import the xlrd module
    import xlrd
    
    #open the spreadsheet file(or workbook)
    workbook = xlrd.open_workbook("test2.xlsx")
    
    #Open a sheet:
    # go into the name of the sheet-below the bottom  testfan is the name
    testfan = workbook.sheet_by_index(0)
    
    
    #extra the value
    print("the value at row 4 and column 0 is:{0}".format(testfan.cell(4,0).value))
    
    
    #the all rows of the sheet
    total_rows = testfan.nrows
    total_cols = testfan.ncols 
    print total_cols
    
    
    #print the 2nd row of the sheet
    print testfan.row_values(1) 
    
    
    
    #get slice of column =2 for row =1...6
    a = testfan.col_values(3,1,6)
    print a
    
    
    #calulate the average num of those numbers
    a = testfan.col_values(3,1,6)
    len(a)
    k = sum(a)/len(a)
    print k
    
    
    #other way of the last record
    output = 0
    
    for x in range(1,3):
        a = testfan.cell(x,5).value
        avg
        print a
相關文章
相關標籤/搜索