将列表转化为字典,再根据字典的数据进行查找
#!coding:utf-8
# Author:pymingming
import xlrd
def read(file, sheet_index=0): #sheet_index=0 读取sheet1,等于1时读取sheet2
workbook = xlrd.open_workbook(file) #将数据存于wookbook
sheet = workbook.sheet_by_index(sheet_index)#sheet的一个索引
#print("工作表名称:", sheet.name, "行数:", sheet.nrows, "列数:", sheet.ncols )
data = []
for i in range(0, sheet.nrows): #从第几行读取,按行读取
data.append(sheet.row_values(i))#将读取的数据追加到列表
return data #返回列表
if __name__ == '__main__': #文件作为脚本直接执行时才会被执行,而import到其他脚本中是不会被执行
message = (read(r'J:\pymingming\day4\生物所.xls')) #输出def read()信息
#print(message)
dictionary = {} #初始化字典 要在def之外,否则报错
def dic(): #字典的填充
for i in message: #遍历列表
dictionary[i[0]] = int(i[1]) #取回字典键值对
return(dictionary)#不断填充字典
#运行def dic(&#x