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.
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.
Example keyword to read a csv file:
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
Example test:
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With