学习目标:
读取用户输入的数据:比如登录账户密码,比如文本框中的内容
实现方法:
使用xlrd读取Excel中的内容,以字典的形式展示
实现方法:
1、需要安装xlrd模块, 打开cmd,输入命令:pip install xlrd 进行安装。
2、使用以下实例,可以直接调用读取
for row in range(1,sheet.nrows):#从第二行开始,获取所有行数
info=sheet.row_values(row)#获取行的内容
tmp=zip(listkey,info)#将列表打包为元组的列表
infolist.append(dict(tmp))#将元祖转为字典----字典型列表
实例:
import xlrd
class Xluserinfo11(object):
def __init__(self,path=''):
# config = codecs.open(path, 'r', 'utf-8')
self.xl=xlrd.open_workbook(path)# 初始化,
def get_sheet_info(self):# 得到数据表中所有数据
listkey=['chaxun']
infolist=[]
for row in range(1,self.sheet.nrows):# 一行行的取数据
info=self.sheet.row_values(row)
tmp=zip(listkey,info)
infolist.append(dict(tmp))
return infolist
def get_sheetinfo_by_name(self,name):#通过Excel表格名的形式取值
self.sheet=self.xl.sheet_by_name(name)
return self.get_sheet_info( )
def get_sheetinfp_by_index(self,index):#通过Excel表格索引方式的形式取值
self.sheet=self.xl.sheet_by_index(index)
return self.get_sheet_info()
if __name__=='__main__':
xinfo = Xluserinfo11(r"C:\Users\junjunbao\Desktop\文档\y.xls")
info = xinfo.get_sheetinfp_by_index(0) # 比如第0个表格
for t in info:
print(t,t["chaxun"])
结果:以字典的形式显示结果