148k views
19 votes
Write a Python 3 program to read from a .csv file containing rates from power companies. Your program should determine the average commercial rate, and also display the information for the highest and lowest rates found.

User Sandover
by
6.6k points

1 Answer

11 votes

Answer:

In python:

file = open("rates.csv", "r")

ratecount = 0

ratesum = 0

mylist = []

rates = file.readlines()

for nums in rates:

num = nums.rstrip('\\')

mylist.append(int(num))

for i in num:

ratesum= ratesum + int(i)

ratecount = ratecount + 1

print("Maximum Rate: "+str(max(mylist)))

print("Minimum Rate: "+str(min(mylist)))

print("Average Rate: "+str(ratesum/ratecount))

Step-by-step explanation:

This opens the csv file

file = open("rates.csv", "r")

This initializes the number of rates in the file

ratecount = 0

This initializes the sum of the rates

ratesum = 0

This initializes an empty list

mylist = []

This reads the rates into lines

rates = file.readlines()

This iterates through the rates (as a string)

for nums in rates:

This removes the newline character \\ in the rates

num = nums.rstrip('\\')

This appends the rates (as an integer) to the empty list

mylist.append(int(num))

The following counts the rates in the file and also sum them up

for i in num:

ratesum= ratesum + int(i)

ratecount = ratecount + 1

This prints the maximum of the rates

print("Maximum Rate: "+str(max(mylist)))

This prints the minimum of the rates

print("Minimum Rate: "+str(min(mylist)))

This calculates and prints the average rate

print("Average Rate: "+str(ratesum/ratecount))

User Jae Kun Choi
by
7.0k points