Question

--------__--------__Python--------__--------__ You will be reading in the data from the file SalesJan2009.csv. When you do, you...

--------__--------__Python--------__--------__

You will be reading in the data from the file SalesJan2009.csv. When you do, you need to save all the items from the PRICE field into a list called amtCollected. After you get them all in that list, you will need to create a variable called total that holds the sum of all the numbers in the list. Then create a variable called avg that holds the average of the numbers in the list. Now, print the following strings: "Total is $(total)" and "Average price is $(avg)"Be sure to replace the (total) and (avg) with the value in the variables. Be sure to do casting where necessary. You may use DictReader to read the data from the csv file and then put that dictionary data into the intended list.

______________________

I'm terribly confused on how to start on this... but this is how I started it

import csv

amtCollected = []
with open("SalesJan2009.csv", "r+") as csv_file:
csv_reader = csv.reader(csv_file, delimiter= ',')
for line in csv_reader:
print(line[2])

------------------------------------

here is the link to the file

https://docs.google.com/spreadsheets/d/1345krckT94BLrBC363nVZCKAh5xoLK_JCCXnMJ_vHEU/edit?usp=sharing

0 0
Add a comment Improve this question Transcribed image text
Answer #1

>>>>>>>>>>>>>>>>>>>>>>>>>>>python code>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

import csv

amtCollected = []

#opening file
csv_file=open("SalesJan2009.csv", "r+")
#read every line and save it to list
csv_file=csv_file.readlines()
csv_file=csv_file[1:]

#spliting every line on delimeter , and taking amount input
for line in csv_file:
   amount=list(line.strip().split(","))[2]
   amtCollected.append(int(amount))

#calculating O/p and printing result
total=sum(amtCollected)
avg=total/len(amtCollected)

print("Total is ${}".format(total))
print("Average price is ${:.2f}".format(avg))

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>sample O/P>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

dilupalludilupalli. Lileyy anupambanupam:~/HomeworkLib$ python3 amount.py Total is $1630500 Average price is $1633.77 Open F anupam

Add a comment
Know the answer?
Add Answer to:
--------__--------__Python--------__--------__ You will be reading in the data from the file SalesJan2009.csv. When you do, you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How to make a table from a csv file without using prettytable or pandas. I have...

    How to make a table from a csv file without using prettytable or pandas. I have to use format I'm trying to... I'm trying to get a column for "Type", "Total", and "Percent" from TipJoke.csv file. import csv with open('TipJoke.csv', newline='') as csv_file: filereader = csv.reader(csv_file, delimiter=' ') for row in filereader: print( '{:2} {:3} {:4}'.format('Type', 'Total', 'Percent')) print(', '.join(row))

  • Write an open_file() and a function that reads a csv data file and returns a dictionary...

    Write an open_file() and a function that reads a csv data file and returns a dictionary Microsoft Word - project09_final_RJE.docx 1/17 This project focuses on analyzing a publicly available dataset containing information about the spread of nCoV. Since this is an active situation, this dataset is constantly being updated with the latest figures. However, for our purposes, we will use a static version of the dataset provided to you with this project (ncov.csv). This static version was lasted updated on...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...

    According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A company has text data that is not...

  • 1. Create a table that will hold the data in avgprice kwh state.csv. This .csv file...

    1. Create a table that will hold the data in avgprice kwh state.csv. This .csv file contains the annual data from 1990-2012 on the average energy price per kilowatt hour (KwH) by state and provider type. Implement the following queries: • Print each row that has Tennessee as state, order the result by year in descending order. • Print the average residential, commercial, industrial, and transportation price for the state of Texas from the year 1990-2012. 2. Create the tables...

  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

  • Write code to read student data from a csv file and add (prepend) that into a...

    Write code to read student data from a csv file and add (prepend) that into a linked list. Assume the code is written in only one file - main.c. You are REQUIRED to write the struct definition for the linked list node. . The input file name is taken from command line argument. . You can write everything except the struct definition in the main function, or can create multiple functions up to you. Following is the sample file data....

  • PLEASE DO IT IN PYTHON, THANK YOU! CSV file: This is a file containing plain text...

    PLEASE DO IT IN PYTHON, THANK YOU! CSV file: This is a file containing plain text where each line in the file represents one record of information, and each piece of info in the record is separated by a single comma. Luckily the values won't ever contain commas themselves for this project. But they might start or end with non visible characters (e.g. space, tab). Thus, you must remove the leading and trailing spaces when you store the data in...

  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT