Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I need the following python code to work with the following instructions. I can't get my code to print valid data to my output file.

I need the following python code to work with the following instructions. I can't get my code to print valid data to my output file. It all goes on as an error in my error file. I can't use pandas, csv module, csv reader, dictionaries or with.

Instructions:

Create a function called load_data() that: o Reads data from the cardiology data set (.csv file). o Parse out the patient id, age, gender, blood pressure, cholesterol and class (Healthy or Sick). You will need to skip the column with angina. o Write the rows with valid data to the output file (named _output6a-xx.txt) where xx is your submission folder number on Google Drive. See Figure 1 for the first few lines of the output file. You will need to reference the bullets below for additional functions used in this function. o Write the patient id of the rows with invalid data to the error file (named _error6a-xx.txt). See Figure 2 for the error file. Note that if the cholesterol, blood pressure or age is non-numeric, there is an error message to that effect. If the class is invalid (not Healthy or Sick), an error message to that effect is displayed. o Returns the number of line written to the output file. ? Use the functions from assignment 3 problem 1, chol_level()and bp_level(). Modify the categories so that they match those shown in Figure 1. For blood pressure, instead of stage 1 hypertension use hypertension1 to avoid spaces in the categories (similarly for stage 2). For cholesterol, instead of borderline high use borderline. Call the functions so that the output file has the category as well as the numeric value for blood pressure and cholesterol as shown in Figure 1. ? Create the function below for changing age to a category and call it to change the age value from numeric to low, medium (med) or high (see Figure 1, first entry for each patient is age category). age_category(): Takes the age as a parameter and returns the category below.

Under 45 = low 45 to 59 = med 60+ = high

Desired output:

Figure 1:

image text in transcribed

Figure 2:

image text in transcribed

My code:

import sys

data = open('cardiology.csv', 'r')

def bp_level(bp):

if (float(bp)

return ('normal')

elif (float(bp) >= 120 and float(bp)

return ('prehypertension')

elif (float(bp) >= 130 and float(bp)

return ('hypertension1')

else:

return ('hypertension2')

def chol_level(ch):

if (float(ch)

return ('desirable')

elif (float(ch) >= 200 and float(ch)

return ('borderline')

else:

return ('high')

def age_category(age):

if (float(age)

return ('low')

elif (float(age) >= 45 and float(age)

return ('med')

else:

return ('high')

def health(hclass):

if hclass.rstrip() == 'Healthy':

return ('Healthy')

elif hclass.rstrip() == 'Sick':

return ('Sick')

output = open('_output6a-09.txt', 'w')

error = open('_error6a-09.txt', 'w')

count = 0

for row in data.readlines()[1:]:

datarow = str(row)

row = datarow.split(',')

try:

bp = bp_level(row[3])

except:

print (row[0], 'bp non-numeric', file = error)

continue

try:

chol = chol_level(row[4])

except:

print (row[0], 'chol non-numeric', file = error)

continue

try:

age = age_category(row[1])

except:

print (row[0], 'age non-numeric', file = error)

continue

try:

healthc = health(row[6])

except:

print (row[0], 'class not valid', file = error)

continue

try:

print (age, row[2], row[3], bp, row[4], chol, healthc, file = output)

count = count + i

except:

continue

output.close()

error.close()

print (count)

output.txt - Notepad Eile Edit Format View Help high Male 130 hypertension1 206 borderline Sick med Male 130 hypertension1 266 high Healthy high Male 110 normal 211 borderline Healthy high Male 130 hypertension1 254 high Sick med Male 140 hypertension2 203 borderline Sick med Female 150 hypertension2 283 high Healthy med Male 120 elevated 284 high Sick med Male 132 hypertension1 224 borderline Sick high Male 160 hypertension2 286 high Sick high Male 120 elevated 229 borderline Sick output.txt - Notepad Eile Edit Format View Help high Male 130 hypertension1 206 borderline Sick med Male 130 hypertension1 266 high Healthy high Male 110 normal 211 borderline Healthy high Male 130 hypertension1 254 high Sick med Male 140 hypertension2 203 borderline Sick med Female 150 hypertension2 283 high Healthy med Male 120 elevated 284 high Sick med Male 132 hypertension1 224 borderline Sick high Male 160 hypertension2 286 high Sick high Male 120 elevated 229 borderline Sick

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions