how to read single column from csv file using python in sikuli

Asked by Kartheek

Hello Raiman,

Initially I found solution for 'finding single row in CSV file in @263844', But now i want to know the solution for 'how to read single column from csv file'. Please help me

Thank you in advance

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1

When creating such more complex solutions, you have to learn some basic Python and not only rely on snippets, you might get from wherever. There are good tutorials available in the net.

the function in the mentioned question returns a list (array in other languages) corresponding to the fields of the addressed row.

theRow = lineFromCSV(file, num)

now you can access each field value by the index starting with 0

print theRow[4]
would print the 5th field value.

Revision history for this message
Kartheek (mkreddy083) said :
#2

yeah.. i referred python tutorials, but while coming to this situation i'm not sure the problem is where. Here i'm pasting my sniipet code kindly look into once and give me the solution.

######
import csv
Col1 = "ColumnName1"
Col2 = "ColumnName2"
Col3 = "ColumnName3"
mydictionary={Col1:[], Col2:[], Col3:[]}
csvFile = csv.reader(open("D:\\Sikuli\\example1.csv", "rb"))
for row in csvFile:
  mydictionary[Col1].append(row[0])
  mydictionary[Col2].append(row[1])
  mydictionary[Col3].append(row[2])

for row in csvFile:
  col1, col2, col3 = row
  print "%s: %s, %s" % (col1, col2, col3)
#####
Here i want to print single column, please provide me solution
Thank you

Revision history for this message
RaiMan (raimund-hocke) said :
#3

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

Revision history for this message
Kartheek (mkreddy083) said :
#4

If you feel anything bad from my side I'm sorry. But I'm trying sincerely please understand my situation.
coming to the issue
I'm getting error on the for loop "for nC in range(length(mycsv[0])):" for this how can i solve..
please give me proper solution

Revision history for this message
RaiMan (raimund-hocke) said :
#5

no problem, but your behavior and what and how you are talking about makes it hard to believe.
Therefor I dare to say things like "please look there and there …" from tim to time.

… and what I meant with believing in snippets you get from wherever:
you first have to try to understand the author's intention and then fix minor problems yourself

in this case it is a minor problem and it would have been very easy, to find out, that instead of
length(some_list_or_string)

in Python it is
len(some_list_or_string)

so in this case you did not:
- show the error message (so I had to guess)
- you did not understand the intention
- and you did not try to fix the problem yourself

nevertheless: come back with any problem (but the complexity of your problems should raise with the time ;-)

Revision history for this message
Kartheek (mkreddy083) said :
#6

Thank you for your suggestion.. but finally I've one more doubt. When i try to run script it is not execution properly and also it is not showing any error.
Please solve this with little bit of patience.

Revision history for this message
Kartheek (mkreddy083) said :
#7

Thanks RaiMan, that solved my question.

Revision history for this message
Kartheek (mkreddy083) said :
#8

please provide me some solution for #6

Revision history for this message
RaiMan (raimund-hocke) said :
#9

please provide more information:
- paste your script
- what do you expect?
- what happens?
- what and how did you do for debugging?

Revision history for this message
Kartheek (mkreddy083) said :
#10

Here is the script,
############
import csv
csvFile = csv.reader(open("D:\\Sikuli\\example1.csv", "rb"))
nR = 1
nC = 1
mycsv = [] # empty list
for row in csvFile:
    mycsv.append(row)

# row nR, column nC
print mycsv[nR][nC]

#gives you the field content of column nC in row nR

# first field in first row:
mycsv[1][1]
#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(len(mycsv[0])):
    mycsvCols.append([])
    for nR in range(len(mycsv)):
        mycsvCols[nC].append(mycsv[nR][nC])

#now the access is:
print mycsv[nR][nC]
################

--> my requirement is to print specific row and column
--> script is not executing and also it is not showing any error

Below i'm pasting My csv file (and it is separated by comma's):

Presedence Sno STP-testcaseno Test_id Scenario Simulator Comport
0 1 STP-GPSBL-001 SimZen-001 general SimZen com1
1 2 STP-GPSBL-002 SimZen-002 general SimZen com2
1 3 STP-GPSBL-003 Simplex-003 gpsblhsiura1 Simplex com1
0 4 STP-GPSBL-004 SimZen-004 gpsblhsiura1 SimZen com1
1 5 STP-GPSBL-005 Accord-005 general1 Accord com3
0 6 STP-GPSBL-006 Ifen-006 general1 Ifen com1

Revision history for this message
Best RaiMan (raimund-hocke) said :
#11

ok, one time service, because I was interested, wether it works.

look at the script content:
https://github.com/RaiMan/SikuliX-2014/tree/master/TestScripts/csvread.sikuli

download the script zipped:
https://raw.githubusercontent.com/RaiMan/SikuliX-2014/master/TestScripts/csvread.sikuli.zip

the csvdata.txt file is contained in the bundle

the script shows everything you need

Revision history for this message
Kartheek (mkreddy083) said :
#12

Thank you very much Raiman

Revision history for this message
Kartheek (mkreddy083) said :
#13

Thanks RaiMan, that solved my question.