Question

In: Computer Science

The file you need to work with contains the data from a student group's last cookie...

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

Solutions

Expert Solution

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


Related Solutions

If the file circuit.txt contains the following data
Exercise 2: If the file circuit.txt contains the following data 3.0             2.1 1.5             1.1 2.6             4.1 The first column is voltage and the second column is the electric current. Write program that reads the voltages and currents then calculates the electric power (P) based on the equation: Voltage     Current        Power 3.0             2.1              (result) 1.5             1.1              (result) 2.6             4.1              (result)                         P = v * i Write your output to the file results.txt with voltage in the first, current in the second...
The file P02_35.xlsx contains data from a survey of 500 randomly selected households. a. Suppose you...
The file P02_35.xlsx contains data from a survey of 500 randomly selected households. a. Suppose you decide to generate a systematic random sample of size 25 from this population of data. How many such samples are there? What is the mean of Debt for each of the first three such samples, using the data in the order given? b. If you wanted to estimate the (supposedly unknown) population mean of Debt from a systematic random sample as in part a,...
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
The file HW_09.xlsx contains data from a survey of 105 randomly selected households (once you click...
The file HW_09.xlsx contains data from a survey of 105 randomly selected households (once you click cell C1, “Ownership,” you can see what 0 and 1 stand for). a. In an effort to explain the variation in the size of the monthly home mortgage or rent payment, estimate a multiple regression equation that includes all of the potential household independent variables. b. Interpret the ANOVA table for this model. In particular, does this set of independent variables provide at least...
For this question you will need to upload a file. It is question 3 from the...
For this question you will need to upload a file. It is question 3 from the PDF file I sent you. Please show your work. The following information is given about the market for a normal good. Demand: P = 150-2Qd Supply P = 20+ 0.5 Qs What is the quantity demanded at a price of $80? (1.5 mark) What is the quantity supplied at a price of $80? (1.5 mark) At a price of $80 the market is not...
On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains...
On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains data on past students in this course. Under Midterm is information on whether past studentsgot an A grade (A−, A, A+) an F or D grade (D is a passing grade but most students need aC− for it to count towards their program) or Other (any grade in between). Under FinalExam is information on whether students got a D or F grade or anything...
The file HW_05.xlsx contains data from a survey of 105 randomly selected households. a. Interpret the...
The file HW_05.xlsx contains data from a survey of 105 randomly selected households. a. Interpret the ANOVA table for this model. In particular, does this set of independent variables provide at least some power in explaining the variation in the dependent variable? Report the F ratio statistics and p- value for this hypothesis test. b. Interpret coefficients of independent variables in the model. c. Using the regression output, determine which of the independent variables should be excluded from the regression...
Assume there is a file called "mydata". each line of the file contains two data items
how do you read in a file in JAVA Assume there is a file called "mydata". each line of the file contains two data items: hours and rate. hours is the represented by the number of hours the worker worked and rate is represented as hourly rate of pay. The first item of data is count indicating how many lines of data are to follow.Methodspay- accepts the number of hours worked and the rate of pay. returns the dollor and cents...
The data file contains displacement (in mm)-load (in N) data for a mechanical test that was...
The data file contains displacement (in mm)-load (in N) data for a mechanical test that was conducted on an unknown metal. The initial length and diameter of the specimen are also given. a. (5 pts.) Using the data and a computer program (such as Excel), create an engineering stress-engineering strain graph with proper labels. The stress axis should be in the units of MPa. You do not need to show your spreadsheet or software code used to make the graph....
In the Excel data file, the tab labeled Question 1 contains data on the number of...
In the Excel data file, the tab labeled Question 1 contains data on the number of times boys and girls raise their hands in class. Conduct the t-test: Two-Sample Assuming Equal Variances. Males 9,8,4,9,3,8,10,8,9,10,7,6,12 Females 3,5,1,2,6,4,3,6,7,9,7,3,7,6,8,8 a. What is the null hypothesis? b. What is the research hypothesis? c. Why run a Two-Sample Assuming Equal Variances t-test? d. Interpret the findings. What are the results of the hypothesis test? Can you reject the null hypothesis?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT