General Question

chk8n's avatar

How to make columns into lists for .csv (excel) with Python?

Asked by chk8n (106points) February 15th, 2011

What I mean by column is when you have:

Student Grade GPA
Max B 3.2
Angel A 3.5
Sonny C 2.1

So column 1, I want: Student Max Angel Sonny
Column 2 I want: Grades B A C
So on….

That is what I have on .csv file.

The code I would need to use is:

StudFile = open(“grades.csv”, “r”)

How to make it read so that I only show the Name of the student AND the GPA only?

Thank you for the help!

Observing members: 0 Composing members: 0

2 Answers

markferg's avatar

data = open(“grades.csv”,“r”).readlines()
for line in data:
[t]items = line.split()
[t]print items[0], items[2]

note that Fluther strips the required tabbing from the program, replace [t] with a real tab. Should add catching exceptions where the file doesn’t exist, or there is not 3 entries on a line. Also relies that the file uses whitespace, not commas to delimit columns. Should really close the file in the program and not leave the OS to sort it out.

Vortico's avatar

For more advanced CSV parsing, use the csv module.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther