0

I have been trying for two hours to create a table of values from a matrix and so far i have been able to create a column from the csv file. I know this is going to be easy for everybody, but when reading from a csv file, i cant seem to phrase it right so would people please put me in the right direction?

import csv

file = open('data.csv', 'rU')
reader = csv.reader(file)

for row in reader:
    print row[0]

so far I can only print out the first column, any advice guys?

1
  • what exactly are you trying to do? print the data.csv? if so, just do print row instead of print row[0] Commented May 8, 2016 at 16:18

2 Answers 2

1

You can do it with a list comprehension:

import csv

with open('data.csv', 'rU') as file:
    table = [row for row in csv.reader(file)]

print(table)

This will create a list of lists where each sublist is a row of the csv file.

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

Comments

0

In your code row is a list of all the columns, So:

row[0] is the first column, row[1] is the second etc.

you can write:

print(str(row))

to print all the columns, or iterate over the with:

for column in row:
    print(column)

2 Comments

thanks man that is much appreciated much easier way to see that actually... so basically after
@entercaspa great. you can mark the answer as correct if its solved your question.

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.