python操作excel,将每行信息放在字典里,所有信息放在一个列表里

xiaoxiao2021-02-28  38

#coding=utf8 from selenium import webdriver import xlrd,os dirname = os.path.dirname(os.path.dirname(__file__)) #join时,第二参数首位不能加/,加r的意思是原生字符串 filename = os.path.join(dirname,r'testdata/select_school.xlsx') #row,col获取哪行那列的值 def run_select_shool(row=1,col=1): #打开excel文件读取数据 data = xlrd.open_workbook(filename) table = data.sheet_by_index(0) row = row-1 col = col-1 #获取整行整列的值 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): # 打开excel文件读取数据 data = xlrd.open_workbook(filename) table = data.sheet_by_index(sheet_index) nrows = table.nrows nclos = table.ncols #获取表头行的信息,为一个列表 header_row_data = table.row_values(table_header_row) #将每行的信息放入一个字典,再将字典放入一个列表中 list = [] for rownum in range(1,nrows): rowdata = table.row_values(rownum) #如果rowdata有值, if rowdata: dict = {} for j in range(0,len(header_row_data)):   #将excel中的数据分别设置成键值对的形式,放入字典,如‘标题’:‘name’; dict[header_row_data[j]] = rowdata[j] list.append(dict) print(list) return list run_select_school2(filename)
转载请注明原文地址: https://www.6miu.com/read-2596170.html

最新回复(0)