1

I'm wanting to read in a csv and save the first two columns as variables. This is what I have so far:

import sys, os, subprocess, shutil, time, string #these are for other things in the program :)
import csv

csvfile = list(csv.reader(open('test.csv')))   #read in the csv file

csv_dic = []

for row in csvfile:
        csv_dic.append(row);

for row in csv_dic:
       for column in row:
             print column, "***",   #I can print out the column
       print

I can print out columns, but would like to have it so I can have different variables for column 'arrays'.

For example, I am wanting to store the first column as 'frames' and the second as 'peaks', so to have it so, for example, frames[2] holds the second value in the first column and I can reference it.

Thank you.

user3295674
  • 893
  • 5
  • 19
  • 42

2 Answers2

3

Here's one way to do it:

frames = []
peaks = []

for row in csv_dic:
    frames.append(row[0])
    peaks.append(row[1])
    for column in row:
        print column, "***",   #I can print out the column
    print

Note that I am making one big assumption: that the two columns you want are exactly the first two columns of the CSV file. But this should do what you want. row[0] is the value in the first column of the row, and likewise, row[1] is the second column.

Now, if you want to do an unknown number of columns, you'll have to use another method (like a dictionary, perhaps).

El'endia Starman
  • 2,204
  • 21
  • 35
  • Thank you, assuming the first 2 columns in the file is perfectly fine. question, if I want to access a specific element in the column, can I do something like row[1][2]? – user3295674 Nov 25 '15 at 23:54
  • Actually, you can't. `row` is a one-dimensional list, so all you can do is `row[1]`. Now, you *can* do something like `csv_dic[4][1]`, where `csv_dic[4]` is the fifth row. – El'endia Starman Nov 25 '15 at 23:56
  • So csv_dic[4][1] will give me the 5th element in the 2nd column? As in, column 2, row 5? – user3295674 Nov 26 '15 at 00:20
  • Ok, so what exactly would something like peaks[1] and peaks[2] hold? – user3295674 Nov 26 '15 at 00:27
  • 1
    `peaks[1]` will have `csv_dic[1][1]`. The second column of the second row. Likewise, `peaks[2]` will have `csv_dic[2][1]`. `frames` is for the first column, so `frames[2]` is `csv_dic[2][0]`. – El'endia Starman Nov 26 '15 at 00:29
1

Simple to do with the CSV module which you are already using. Here is a dictionary method and an array method.

dictionary

import csv
var1 = "frames"
var2 = "peaks"
csv_dic={var1:[], var2:[]}
csvFile = csv.reader(open('test.csv', 'rb'))
for row in csvFile:
  csv_dic[var1].append(row[0])
  csv_dic[var2].append(row[1])

again assuming that you only are worried about first two elements of each row.

Here is a method that will get you everything into your array, here called data. This is relatively fast for large files.

array

import csv
with open('test.csv','rb') as file:
    rows = csv.reader(file, 
                      delimiter = ',', 
                      quotechar = '"')
    data = [data for data in rows]
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51