利用xlrd处理Excel文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import  xlrd
 
workbook  =  xlrd.open_workbook(datafile)
 
sheet  =  workbook.sheet_by_index( 0 )
 
data  =  [[sheet.cell_value(row,col)  for  col  in  xrange (sheet.ncols)]  for  row  in  xrange (sheet.nrows)]
 
#sheet 的row index和 col index 都是从0开始的
 
coast  =  sheet.col_values( 1 ,start_rowx = 1 ,end_rowx = None ) # coast 是一个列表 行数范围[start,end)
 
#时间处理
 
exceltime  =  '1/1/13 1:00'
 
xlrd.xldate_as_tuple(exceltime, 0 ) #结果为 (2013,1,1,1,0,0)精确到秒
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用`xlrd`库遍历Excel表并使用`pytest`进行数据驱动测试,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了`xlrd`和`pytest`库。你可以使用以下命令通过pip安装它们: ```shell pip install xlrd pip install pytest ``` 2. 创建一个Excel文件,其中包含你的测试数据。假设你的Excel文件名为`test_data.xlsx`,并且数据位于名为`Sheet1`的工作表中。 3. 创建一个名为`test_data.py`的Python文件,并在其中编写测试用例和测试数据。以下是一个示例: ```python import xlrd import pytest # 读取Excel文件 def read_excel_file(file_path): workbook = xlrd.open_workbook(file_path) sheet = workbook.sheet_by_name('Sheet1') data = [] for row in range(1, sheet.nrows): test_case = { 'input': sheet.cell(row, 0).value, 'expected_output': sheet.cell(row, 1).value } data.append(test_case) return data # 测试用例 @pytest.mark.parametrize('test_data', read_excel_file('test_data.xlsx')) def test_my_function(test_data): input_data = test_data['input'] expected_output = test_data['expected_output'] # 执行测试逻辑,比如调用待测试的函数或方法 actual_output = my_function(input_data) # 断言实际输出与期望输出是否一致 assert actual_output == expected_output ``` 在上面的示例中,`read_excel_file`函数用于读取Excel文件并返回测试数据列表。`test_my_function`是一个测试用例函数,使用`@pytest.mark.parametrize`装饰器将测试数据作为参数传递给该函数。在测试用例函数中,你可以执行你的测试逻辑并使用断言来验证实际输出与期望输出是否一致。 请确保根据你的实际情况修改上述代码中的文件路径、工作表名称和列索引。 4. 运行测试。在命令行中进入包含`test_data.py`文件的目录,并运行以下命令: ```shell pytest ``` `pytest`将自动运行测试用例,并根据测试结果提供相应的输出。 这样,你就可以使用`xlrd`库遍历Excel表并利用`pytest`进行数据驱动测试了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值