HandleExcel.py
1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/7/16 下午5:38
# @Author : 付孟奇
import xlrd
'''
Created on 2017-4-26
读取excel文件,将excle文件里面对应的sheet of addbook数据转换为python 列表对象,其中列表中每一个元素为一个字典
'''
def excel_to_list(file, tag):
"""将excel表中数据转换成python对象"""
data_list = []
"""打开excel文件"""
book = xlrd.open_workbook(file)
"""选择读取sheet工作表"""
tag = book.sheet_by_name(tag)
"""获取行数"""
row_num = tag.nrows
header = tag.row_values(0)
for i in range(1, row_num):
"""读取行"""
row_data = tag.row_values(i)
"""读取行中的每一列的值"""
d = dict(zip(header, row_data))
data_list.append(d)
return data_list
"""获取测试数据,判断传入的test_name 是否存在,存在则返回一个列表中对应的字典数据"""
# 无效方法
def get_test_data(test_name, test_list):
for test_dict in test_list:
if test_name == test_dict['test_name']:
return test_dict
if __name__ == '__main__':
excel_to_list(1, 1)