Having an issue calculating age using csv file without importing it completely into memory

39 Views Asked by At

I keep getting the following error. don't know how to fix it.

TypeError: '_csv.reader' object is not subscriptable

.....

def median_age(filename):
    
    with open(filename, 'r') as file:
        
        data = csv.reader(file,delimiter = '\t')
        
        next(data) # Skips the header of the csv file
        
        customer_age = (2024 - int(data['birth_year']) for row in data)
        
        median_customer_age = statistics.median(customer_age)
        
        return median_customer_age
1

There are 1 best solutions below

0
DarrylG On

Issue

  • With csv.reader object, each row is a list of the field values.
  • Since you desire to reference values by fieldnames you should use DictReader which provides a dictionary rather than a list.
  • DictReader maps the information in each row to a dictionary whose keys correspond to the fieldnames.

Code

import csv
from statistics import median

def median_age(filename):
  with open(filename, 'r') as file:
    reader = csv.DictReader(file, delimiter = '\t')

    # Generator for ages
    # row is dictonary of fieldnames/values pairs
    customer_age = (2024 - int(row['birth_year']) for row in reader)
             
    return median(customer_age)

Example Usage

Input File: data.csv

first   last    birth_year
paul    henry   2019
bill    thompson    1995
mary    allen   2003
jennifer    davis   2015
liz morgan  1999

Test Code

median_customer_age = median_age('data.csv')
print(f'File data.csv median age: {median_customer_age}')

Output

File data.csv median age: 21