xlrd encapsulated data read excel

 

# Coding: UTF. 8- 
Import to xlrd
 class ExcelUtil ():
     DEF  the __init__ (Self, excelPath, sheetName = " Sheet1 " ): 
        self.data = xlrd.open_workbook (excelPath) 
        self.table = self.data.sheet_by_name (sheetName)
         # obtaining a first row as the key value 
        self.keys = self.table.row_values (0)
         # Get the number of rows 
        self.rowNum = self.table.nrows
         # acquires the total number of columns 
        self.colNum = self.table.ncols 

    DEF dict_data (Self ):
         IFself.rowNum <=. 1 :
             Print ( " total number of lines is less than. 1 " )
         the else : 
            R & lt = [] 
            J =. 1
             for I in List (Range (-self.rowNum. 1 )): 
                S = {}
                 # from the second row to take values corresponding to the value of 
                # S [ 'rowNum'] = I + 2 
                values = self.table.row_values (J)
                 for X in List (Range (self.colNum)): 
                    S [self.keys [X]] = values [X ] 
                r.append (s)
                J + =. 1
             return R & lt 



IF  the __name__ == " __main__ " : 
    filepath = R & lt " C: \ the Users \ Administrator \ Desktop \ test \ documents \ Akei \ credit card test data \ test_data.xlsx " 
    sheetName = " Sheet1 " 
    Data = ExcelUtil (filepath, sheetName)
     Print (data.dict_data ())

 operation result:

[{ 'Name Supplier': '222 Lin Ai', 'channel code': 452369.0, 'the bank of': 'SPDB'}]

 

NOTE: This should be a channel code string when the data is read into a xlrd float, need to be addressed separately.

 

Guess you like

Origin www.cnblogs.com/wenm1128/p/11534205.html