Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a csv file using python with headers intact, where first column is a non-numerical

Tags:

python

csv

This is an elaboration of a previous question, but as I delve deeper into python, I just get more confused as to how python handles csv files.

I have a csv file, and it must stay that way (e.g., cannot convert it to text file). It is the equivalent of a 5 rows by 11 columns array or matrix, or vector.

I have been attempting to read in the csv using various methods I have found here and other places (e.g. python.org) so that it preserves the relationship between columns and rows, where the first row and the first column = non-numerical values. The rest are float values, and contain a mixture of positive and negative floats.

What I wish to do is import the csv and compile it in python so that if I were to reference a column header, it would return its associated values stored in the rows. For example:

>>> workers, constant, age >>> workers     w0     w1     w2     w3     constant     7.334     5.235     3.225     0     age     -1.406     -4.936     -1.478     0 

And so forth...

I am looking for techniques for handling this kind of data structure. I am very new to python.

like image 795
myClone Avatar asked Aug 06 '10 23:08

myClone


People also ask

Is header file as first line CSV?

In the first line of the file, include a header with a list of the column names in the file. This is optional, but strongly recommended; it allows the file to be self-documenting.


1 Answers

For Python 3

Remove the rb argument and use either r or don't pass argument (default read mode).

with open( <path-to-file>, 'r' ) as theFile:     reader = csv.DictReader(theFile)     for line in reader:         # line is { 'workers': 'w0', 'constant': 7.334, 'age': -1.406, ... }         # e.g. print( line[ 'workers' ] ) yields 'w0'         print(line) 

For Python 2

import csv with open( <path-to-file>, "rb" ) as theFile:     reader = csv.DictReader( theFile )     for line in reader:         # line is { 'workers': 'w0', 'constant': 7.334, 'age': -1.406, ... }         # e.g. print( line[ 'workers' ] ) yields 'w0' 

Python has a powerful built-in CSV handler. In fact, most things are already built in to the standard library.

like image 147
Katriel Avatar answered Sep 30 '22 09:09

Katriel