Python operates excel, putting each row of information in a dictionary, and all information in a list

#coding=utf8
from selenium import webdriver
import xlrd,os



dirname = os.path.dirname(os.path.dirname(__file__))
When #join, the first position of the second parameter cannot be added /
 filename = os.path.join(dirname, r'testdata/select_school.xlsx' )

#row,col get the value of which row and column
 def run_select_shool(row= 1 ,col= 1 ):
     #Open excel file to read data
     data = xlrd.open_workbook(filename)
    table = data.sheet_by_index(0)

    row = row- 1
     col = col- 1
 #Get the value of the entire row and column
 nrows = table.row_values(row)
     ncols = table.col_values( 0 )
     print (nrows[col])        

def run_select_school2(filename,sheet_index= 0 ,table_header_row= 0 ):
     # Open excel file to read data
     data = xlrd.open_workbook(filename)
    table = data.sheet_by_index(sheet_index)
    nrows = table.nrows
    nclos = table.ncols

    #Get the information of the header row
     header_row_data = table.row_values(table_header_row) #Put
     the information of each row into a dictionary, and then put the dictionary into a list
     list = []
     for rownum in range ( 1 ,nrows):
        rowdata = table.row_values(rownum)
        #If rowdata has a value,
 if rowdata:        
            dict = {}
            for j in range(0,len(header_row_data)):
                dict[header_row_data[j]] = rowdata[j]
            list.append(dict)
    print(list)
    return list



run_select_school2(filename)



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325841750&siteId=291194637