In: Computer Science
The file you need to work with contains the data from a student group's last cookie sale. The data represents the number of dozens of each type of cookie that were made by each person on the bake sale team.
The organization needs summary information on both the bakers and the cookies.
Write a function named cookieSummary with one parameter, a string that will be the name of a bake sale file (our test case for this assignment is bakeSaleSmall.csv). Your function must work for any file that has this same structure - so you must use the function parameter to pass in the file name.
Compute the number of dozens of cookies produced by each person. Compute the number of dozens of each type of cookie that was produced.
Write a new csv file named bakeSaleSummary.csv that contains all of the original bake sale information as well as: a new column, with header = 'Total', that has the number of dozens of cookies baked by each person a new row, with header = 'Total', that has the number of dozens of each type of cookie that is baked
Be sure to close the file when you are finished.
Include a function call to your own function within your file. The parameter value should be obtained by using the input() function before being used in the function call.
Ex: If the input is:
bakeSaleSmall.csv
and the contents of bakeSaleSmall.csv are:
Person/Cookie,Chocolate Chip,Peanut Butter Marcus,15,20 Oliver,8,12
The file bakeSaleSummary.csv should contain:
Person/Cookie,Chocolate Chip,Peanut Butter,Total Marcus,15,20,35 Oliver,8,12,20 Total,23,32,55
PYTHON CODE PLEASE
Complete code in python3:-
import csv
# This Function reads from file
# And creates new file.
def cookieSummary(filename):
# 'lines' will store the all rows written in input
file
lines = []
# Reading input file.
with open(filename, "r") as file:
# Creating reader object to read
from file.
readerObj = csv.reader(file)
# Reading header row of input
file.
headers = next(readerObj)
# Reading all data lines
for line in readerObj:
lines.append(line)
# Closing file
file.close()
# 'totalcookie' is the last row that will be added to
the summary file.
totalcookie = [0 for i in range(len(headers)+1)]
# Last row's first column is "Total"
totalcookie[0] = "Total"
# Appending new column in summary file
headers.append("Total")
# Calculating sum of cookie for each person
# And appending this sum into each row as value for
new cerated column.
for i in range(len(lines)):
total = 0
j = 1
for idx in range(1,
len(lines[i])):
total +=
int(lines[i][idx])
totalcookie[j]
+= int(lines[i][idx])
j += 1
lines[i].append(str(total))
# Creating last row for summary file.
total = 0
for i in range(1, len(totalcookie)):
total += totalcookie[i]
totalcookie[i] =
str(totalcookie[i])
totalcookie[-1] = str(total)
# Appending last row.
lines.append(totalcookie)
# Opening new file in write mode.
with open("bakeSellSummary.csv", "w") as file:
writerObj = csv.writer(file)
writerObj.writerow(headers)
writerObj.writerows(lines)
file.close()
# Asking for file name from user
filename = input("Enter file name: ")
# Calling function for creating reading input file and creating new
file.
cookieSummary(filename)
Input 'csv' file content:-
Person/Cookie,Chocolate Chip,Peanut Butter
Marcus,15,20
Oliver,8,12
Output 'csv' file content:-
Person/Cookie,Chocolate Chip,Peanut Butter,Total
Marcus,15,20,35
Oliver,8,12,20
Total,23,32,55