简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM