Question
Modify this python program- Read the BMI file from before (since we cannot attach files, please generate random values to demonstrate that the program works).
Modify this python program-
Read the BMI file from before (since we cannot attach files, please generate random values to demonstrate that the program works). Filter the data to output to a file only patients that are classified as obese or underweight, and output those two sets to different csv files.Specifically, in base Python, make your own class for reading .csv files. Define a class called Patient. Feed one line from the CSV into Patient at a time, into the class constructor, which should parse the line and set the variables Gender, Height and Weight. You should construct a list of instances of class Patient from all the data in the CSV file. Additionally, the class should have a method bmi to calculate and store a column associated with bmi, then another method which classifies the health status (using bmi) as health, overweight or underweight. Finally have a method which prints the data from the class out to a file, one index of the list of your class at a time. Save this program as "hw5_q1.py"
import csv import matplotlib.pyplot as plt
class bmi: def __init__(self): self.filename='bmi.csv'; #opening file def readFile(self): obj=open(self.filename) obj_reader=csv.reader(obj,delimiter=' ') male=[] # having male's bmi female=[] # having female bmi file_obj2=open("bmi_health.csv","w") # opening file for adding bmi and status for val in obj_reader: bmi=(int(val[1])*2.2*703)/(int(val[2])*int (val[2])) # bmi calculation bmi=round( bmi,2) file_obj2.write(val[0]) file_obj2.write(' ') file_obj2.write(val[1]) file_obj2.write(' ') file_obj2.write(val[2]) file_obj2.write(' ') file_obj2.write("%0.2f "%bmi) file_obj2.write(' ') if val[0]=='male': # checking for male male.append(bmi) if bmi>=19 and bmi<=25: # bmi healthy status file_obj2.write('healthy') elif bmi>25: file_obj2.write('obese') else: file_obj2.write('underweight') else: # checking for male female.append(bmi) if bmi>=19 and bmi<=25: # bmi healthy status file_obj2.write('healthy') elif bmi>25: file_obj2.write('obese') else: file_obj2.write('underweight') file_obj2.write(' ') print(" BMI list for male: ") print(male) # male bmi calculation list print(" BMI list for female: ") print(female) #female bmi calculation list obj.close() # closing old file file_obj2.close() #closing new file plt.ylabel('NUMBER') plt.xlabel('BMI') plt.title('HISTROGRAM OF BMI OF MALE & FEMALE ') plt.hist([male,female]) plt.show() ob=bmi() ob.readFile()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started