sikuli-driver team mailing list archive
-
sikuli-driver team
-
Mailing list archive
-
Message #31184
Re: [Question #263857]: how to read single column from csv file using python in sikuli
Question #263857 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/263857
Status: Open => Answered
RaiMan proposed the following answer:
this is overkill ;-)
- a csv file has rows (the 1st usually contains the field headers)
- each row has one or more fields separated by the separator
- the python module has various other options, to set the cvs appearance for correct reading
... but basically it is a list of rows, where each row contains a list of fields and each field at a specific position in a row has the same meaning but usually different content than in other rows.
Usually like a with 2-dim table in Excel, the column structure is fixed and known.
... this can be transformed in a 2-dimensional array or speaking Python
a list of lists.
so if you want to have random access to any field in any row and the
file is small to medium sized (some MB), then this is the basic
preparation:
csvFile = csv.reader(open("D:\\Sikuli\\example1.csv", "rb"))
mycsv = [] # empty list
for row in csvFile:
mycsv.append(row)
Now you have your complete cvs file as 2-dim list in memory and can
access it:
# row nR, column nC
print mycsv[nR][nC]
gives you the field content of column nC in row nR
Be aware: in Python like in many other languages we count from 0
# first field in first row:
mycsv[0][0]
you might now of course convert the list, so a list row contains all
fields of all rows (mycsv inverted)
mycsvCols = []
for nC in range(length(mycsv[0]):
mycsvCol.append([])
for nR in (length(mycsv)):
mycsvCol[nC].append(mycsv[nR, nC])
now the access is:
print mycsv[nC][nR]
... and you might of course decide to already get the inverted list
already when doing the initial csv read.
... many options, but still basic Python and even basic array/list
processing
--
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.