How to extract certain header based csv data in python

How to extract specific data from csv file based on header in python? For example, say a csv file contains the following information:

Height,Weight,Age 6.0,78,25 

How could I get only age in python?

+4
source share
2 answers

The second recommendation is csv , but I think using csv.DictReader will be easier:

(Python 2):

 >>> import csv >>> with open("hwa.csv", "rb") as fp: ... reader = csv.DictReader(fp) ... data = next(reader) ... >>> data {'Age': '25', 'Weight': '78', 'Height': '6.0'} >>> data["Age"] '25' >>> float(data["Age"]) 25.0 

Here I used next only to get the first row, but you could iterate over the rows and / or retrieve the full column of information if you want.

+6
source

The next process is: read in the first row, find the index (location) in this row of data that you are looking for, then use this index to pull data from the remaining rows.

Python offers a very useful csv.reader class to do all the reading, so it's pretty simple.

 import csv filename = 'yourfilenamehere' column = 'Age' data = [] # This will contain our data # Create a csv reader object to iterate through the file reader = csv.reader( open( filename, 'rU'), delimiter=',', dialect='excel') hrow = reader.next() # Get the top row idx = hrow.index(column) # Find the column of the data you're looking for for row in reader: # Iterate the remaining rows data.append( row[idx] ) print data 

Note that the values ​​will be displayed as strings. You can convert to int, wrapping row[idx] for example. data.append( int( row[idx] ) )

+1
source

All Articles