0

I want to store the values of a single row and its columns into different variables. But i don't know whether it is possible or not.

Here I'm pasting my code, please look into it

import csv

csvFile = csv.reader(open("D:\\Sikuli\\example1.csv", "rU"))
mycsv = [] # empty list

for row in csvFile:
    mycsv.append(row)
    print row
    print "....."
    print row[1]

In this case I'm able to print only row, I'm unable to store the data into different variables. please provide me solution. Thank you in advance

my csv file is:

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
martineau
  • 119,623
  • 25
  • 170
  • 301
Kartheek
  • 35
  • 6
  • Can you give an example of your csv file and your expected output? – Paul Rooney Mar 24 '15 at 11:37
  • A row is like a list, cannot you store the data through accessing row[1] row[2] row[3] building it with the structure you want? – lapinkoira Mar 24 '15 at 11:39
  • @Paul Rooney, i have updated my question and i'm expecting I want store the value of a row and its respective columns data into individual variables – Kartheek Mar 24 '15 at 12:08

3 Answers3

4

Use csv.DictReader and be happy

import csv

with open("D:\\Sikuli\\example1.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['Presedence'], row['Sno'], row['STP-testcaseno'])

0 1 STP-GPSBL-001
1 2 STP-GPSBL-002
1 3 STP-GPSBL-003
...

About the question in comments for filtering by value.

for row in reader:
    if row['Presedence'] == 'Some Value':
        print(row['Presedence'], row['Sno'], row['STP-testcaseno'])
    else:
        print(row['Sno'], row['STP-testcaseno'])
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
  • Hi @Mauro, When i run the above script i got the below error please look into once csvFile = csv.DictReader(open()) TypeError: file() takes 1-3 arguments (0 given) – Kartheek Mar 24 '15 at 12:51
  • @Kartheek sorry, fixed. – Mauro Baraldi Mar 24 '15 at 12:59
  • yeah its working thank you @Mauro Baraldi and I've one more doubt i.e based on precedence how can i proceed. "if precedence is one then only i want read the specific column info else go to next row" – Kartheek Mar 24 '15 at 13:08
  • @Kartheek it is iterating over rows. You can choice which column want to print or handle with it! – Mauro Baraldi Mar 24 '15 at 13:19
  • OK, but is there any chance like that.. means if (precedence = 1) then print the row data else go to the next field – Kartheek Mar 24 '15 at 13:34
1

You could do something like this

import csv

csvFile = csv.reader(open("D:\\Sikuli\\example1.csv", "rU"))
mycsv = {} # empty dictionary

for row in csvFile:
    mycsv["col_field_name1"] = row[1]
    mycsv["col_field_name2"] = row[2]
    ...

Also here you have other example about creating a dictionary from a csv file Creating a dictionary from a csv file?

Community
  • 1
  • 1
lapinkoira
  • 8,320
  • 9
  • 51
  • 94
  • `mycsv = {}` creates an empty dictionary, not a list. – martineau Mar 24 '15 at 12:05
  • Of course it does, has the title says "store the values of a single row and its columns into different variables in python" a dictionary is a clearer way to store variables (pair key value) instead a list – lapinkoira Mar 24 '15 at 12:09
0

I am explaining with an example:

let eggs.csv without header name

0@mailinator.com,0fname,0lname,0place
1@mailinator.com,1fname,1lname,1place
2@mailinator.com,2fname,2lname,2place
3@mailinator.com,3fname,3lname,3place

Then

import csv
with open('eggs.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')
    for i in spamreader:
        print i # you can append this to your list

>>>['10@mailinator.com', '10fname', '10lname', '10place']
   ['11@mailinator.com', '11fname', '11lname', '11place']
   ...

If you have header in eggs.csv like:

email,firstname,lastname,place
0@mailinator.com,0fname,0lname,0place
1@mailinator.com,1fname,1lname,1place
2@mailinator.com,2fname,2lname,2place
3@mailinator.com,3fname,3lname,3place

Then you can use DictReader like

with open('eggs.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row) #you can append this

>>>{'lastname': '0lname', 'place': '0place', 'email': '0@mailinator.com', 'firstname': '0fname'}
   {'lastname': '1lname', 'place': '1place', 'email': '1@mailinator.com', 'firstname': '1fname'}
   ...

Note

I am using , as delimiter since values are seperated with , in eggs.csv

itzMEonTV
  • 19,851
  • 4
  • 39
  • 49