如何读取机器人框架中的csv文件进行数据验证 [英] how to read the csv file in robot framework for data verification

查看:571
本文介绍了如何读取机器人框架中的csv文件进行数据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取CSV档案以进行资料验证。任何用于读取CSV文件的库或关键字都可以。

I want to read a CSV file for data verification. Any library or Keywords for reading CSV file would do. I am using Robot Framework with Ride.

推荐答案

您可以在python中轻松地创建自己的库来读取和写入csv文件。这样,您可以创建任何所需的关键字。您可以简单地读取并返回所有数据,或者返回一个包含行数或列数的关键字。

You can easily create your own library in python for reading and writing csv files. Doing so lets you create any keywords you want. You could simply read and return all the data, or have a keyword that returns the number of rows, or the number of columns, or anything else.

示例关键字以读取csv文件:

将以下定义保存在名为 csvLibrary.py 。它创建一个关键字库,其中包含一个名为read csv file的关键字。 pass是csv文件的路径,它将以列表列表的形式返回数据。

Save the following definition in a file named csvLibrary.py. It creates a keyword library with a single keyword named "read csv file". Pass is the path to a csv file and it will return the data as a list of lists.

import csv
class csvLibrary(object):

    def read_csv_file(self, filename):
        '''This creates a keyword named "Read CSV File"

        This keyword takes one argument, which is a path to a .csv file. It
        returns a list of rows, with each row being a list of the data in 
        each column.
        '''
        data = []
        with open(filename, 'rb') as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
                data.append(row)
        return data

示例测试:

此测试将使用csvLibrary打开一个.csv文件,读取它并将结果作为列表列表返回: / p>

This test will use the csvLibrary to open up a .csv file, read it, and return the result as a list of lists:

*** Settings ***
| Library | csvLibrary.py

*** Test cases *** 
| Reading a csv file
| | ${data}= | read csv file | test.csv
| | log | ${data}

这篇关于如何读取机器人框架中的csv文件进行数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆