Sheena Wadhwa
Sheena Wadhwa

Reputation: 45

Read specific column of csv

The code is working fine but it is creating list of values in braces. I want to modify the code in such a way that it prints as in csv in proper column and row format.

Expected output :

Ver Total 

4     5

4     5

4     5

4     5

Actual Output:

(ver,total) (4,5) (4,5) (4,5)

Here is the following code

import csv
f = open("a.csv", 'r')
reader = csv.reader(f)
data = []
for line in f:
    cells = line.split(",")
    data.append((cells[0], cells[3]))
print data

Upvotes: 1

Views: 474

Answers (1)

Ranit Das
Ranit Das

Reputation: 64

Try this code:

import csv

with open('a.csv') as csvfile:
    reader = csv.reader(csvfile)
    rowcnt = 0
    for row in reader:
        if rowcnt == 0:
            print row[0], row[1]
        else:
            print row[0], '  ', row[1]
        rowcnt = rowcnt + 1

Provides the following output:

Ver Stat
4    5
4    5
4    5

Upvotes: 1

Related Questions