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.

7
  • 1
    better to tokenize to a dict and use the key's as your names and the values as your columns Commented Nov 25, 2015 at 23:50
  • Is it important that these be variables or should they be members of a dict? Do you know the names of the variables before hand? Commented Nov 25, 2015 at 23:51
  • @tdelaney it would only make sense that all the variables on the same column will have the same variable name and different values - doesn't it ? Commented Nov 25, 2015 at 23:52
  • @alfasin sure, but that has nothing to do with my question. Commented Nov 25, 2015 at 23:53
  • Its common for csv files to have a header line that is then used as a column name. Is that the case here? Commented Nov 25, 2015 at 23:54

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

5 Comments

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]?
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.
So csv_dic[4][1] will give me the 5th element in the 2nd column? As in, column 2, row 5?
Ok, so what exactly would something like peaks[1] and peaks[2] hold?
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].
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]

1 Comment

What if I want to store all the elements (in a large number of rows) for the first two columns?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.