# -*- coding: gbk -*-
import csv
import pandas
import xlrd
class ReadReqParams:
"""读测试用例文件"""
def __init__(self, file_path):
self.file_path = file_path
def read_csv_file(self):
"""读CSV格式文件"""
try:
with open(self.file_path, 'r', encoding='gbk') as f:
reader = csv.reader(f)
fieldnames = next(reader)
csv_reader = csv.DictReader(f, fieldnames=fieldnames)
file_list = []
for row in csv_reader:
params_one = {}
for k, v in row.items():
params_one[k] = v
file_list.append(params_one)
return file_list
except FileNotFoundError:
return ('未找到测试文件')
def read_xlsx_file(self):
"""使用 pandas读excle文件"""
try:
wb = pandas.read_excel(self.file_path)
except FileNotFoundError:
return '未找到测试用例文件'
else:
file_list = []
for i in wb.index.values:
row_data = wb.loc[i].to_dict()
file_list.append(row_data)
# excel填写为空时,读取为None
return file_list
def read_excel_file(self):
"""读excle文件"""
try:
data = xlrd.open_workbook(self.file_path)
table = data.sheet_by_index(0)
except FileNotFoundError:
return '未找到测试用例文件'
except xlrd.biffh.XLRDError:
print('未找到sheet')
return '未找到测试用例文件'
else:
keys = table.row_values(0)
rownum = table.nrows
colnum = table.ncols
if rownum <= 1:
print('未找到有效的测试用例')
else:
file_list = []
for i in range(rownum-1):
data_list = {}
i += 1
values = table.row_values(i)
for x in range(colnum):
data_list[keys[x]] = values[x]
file_list.append(data_list)
# excel填写为空时,不会转换为null
return file_list
python读取excel\csv测试用例文件
最新推荐文章于 2023-10-15 18:29:26 发布