CSE-8A / PA3 / Pa 3 test.py
Pa 3 test.py
Raw
#list1 = ["Males", "Females", "Males", "Males", "Males", "Females", "Females"]
#list2 = list1
#num_males = len(list1) # shows the number of stuff in list1


#num_males = list1.count("Males")
# BEGIN PROVIDED CODE
# ingest_data: given a file name and a field name, returns a list containing
# all the data in the given file for the given field name
import csv
def ingest_data(filename, fieldname):
    file_object = open(filename, newline='')
    rows = csv.reader(file_object, delimiter=',')
    headers = next(rows)
    try:
        field_idx = headers.index(fieldname)
    except ValueError:
        print('The field name', fieldname, 'does not exist in the headers.')
        print('Here are the value field names in this file:')
        for h in headers:
            print(h)
        return
    data_list = []
    count = 0
    limit = 2000 # CHANGE LIMIT
    for line in rows:
        if (count >= limit):
            print('Too many entries, returning first', limit, 'entries.')
            return data_list
        try:
            field_value = line[field_idx]
        except IndexError:
            print('Skipping row #', count, 'because field does not exist')
            continue
        data_list.append(field_value)
        count = count + 1
    return data_list

Sex = ingest_data("data.csv", "Sex")
DescriptionofInjury = ingest_data("data.csv", "DescriptionofInjury")
Location = ingest_data("data.csv", "Location")

list1 = Sex
list3 = Location

num_hospital = list3.count("Hospital")
num_residence = list3.count("Residence")
num_other = list3.count("Other")

num_males = list1.count('Male')
num_females = list1.count('Female')

print ("The total number of males out of 2000 people is:", num_males)
print("The total number of females out of 2000 people is:", num_females)
print("The number of people who are unidentified is:", 2000 - (num_males + num_females))