Loop through CSV file and incrementing variable

Asked by Thomas on 2017-03-15

I am using Sikuli 1.1.0
I have a a set of static data that I have put into a csv.
The csv has 3 columns and upto 20 rows.
I wish to apply each of the cells to a variable in a script which will do some testing.
I have been able to assign the first row to the variables I need to use but I have got myself completely confused on how to increment the selection by 1 row, so that when my first set of tests have completed I can repeat the same script with the next row of variables.

import csv

class ContactDetails():
    def __init__(self, filename):
        with open(filename, "r") as f_input:
            csv_input = csv.reader(f_input)
            self.details = list(csv_input)

    def get_col_row(self, col, row):
        return self.details[row-1][col-1]

data = ContactDetails("C:\\testclist.csv")
csvfile = list(csv.reader(open('C:\\testclist.csv')))

#csvdics = []

for row in csvfile:
    Cruise = data.get_col_row(1, 2)
    Cell = data.get_col_row(2,2)
    cruisenum = data.get_col_row(3,2)
    wait (4)
    # start booking process direct into form
    openApp(r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
    wait(5) # waits 5 seconds, I increased the wait to 7 seconds - just to be safe
    type("l", KeyModifier.CTRL) # press lowercase L + CTRL
    print Cell
    print cruisenum

print "%s %s: %s" % (Cruise, Cell, cruisenum)

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
Best RaiMan (raimund-hocke) said : #1

since you are (correctly) using:
for row in csvfile:

you have to refer to row inside the loop and not data.

row is a python list, hence you access the row fields as row[n]

instead of using
data.get_col_row(1, 2)
inside the loop.

In my understanding of the csv module:

csv_input = csv.reader(open(filename))
for row in csv_input:
    print row, row[1], ...

according to:

Thomas (1bitzer) said : #2

Thanks RaiMan, that solved my question.

Thomas (1bitzer) said : #3

Thanks so much, got myself into confusion and pretty sure this has resolved it as can do several steps. Many many thanks