Question

In: Computer Science

A hotel salesperson enters sales in a text file. Each line contains the following, separated by...

  1. A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event.

Write a program that reads such a file and displays the total amount for each service category. Use the following data for your text file:5 pts

Bob;Dinner;10.00;January 1, 2013

Tom;Dinner;14.00;January 2, 2013

Anne;Lodging;125.00;January 3, 2013

Jerry;Lodging;125.00;January 4, 2013

The output should be:

Dinner: 24.00

Lodging: 250.00

Python, keep it simple, thank you

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

code.py

def main():
  
#name of the file
#fileName = input("Enter file name : ")
fileName="sales.txt"
  
#try block to avoid any run time exception
try:
  
#store all sale service wise
allSale={}
  
#open file with
with open(fileName) as f:
  
#read all lines from the file
content = f.readlines()
  
#strip all space from the file
content = [x.strip() for x in content]
  
#for each row in content
for record in content :
  
#split row by comma
records= record.split(";");
  
#now service will be at index 1 and bill amount will be at index 2
if(records[1] in allSale):
allSale[records[1]]=allSale[records[1]]+float(records[2])
else:
allSale[records[1]]=float(records[2])
  
#for each key in sale
for key in allSale:
print(key,":",allSale[key])
except:
print("Error in opening file")
return []
main()


Related Solutions

An HTML file is a text file that contains text to be displayed in a browser...
An HTML file is a text file that contains text to be displayed in a browser and __________ to be interpreted (read) by the browser formatting and styling the document Both C++ and javascript are computer programing languages; what are their differences? What HTML tag can let you insert javascript in to document Which attributes of the <script> tag tells the brorwser what kind of scripting language is insterted? (give an example) in the javascript section of the HTML file,...
Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
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...
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
assignment in C I have a file that contains a lot of lines with words separated...
assignment in C I have a file that contains a lot of lines with words separated by spaces ( also contains empty lines as well). I need to read this file line by line and put each word into 2d array. NOTE: i need to skip spaces as well as empty lines. also I need to count each word.
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...
On Python Preview the provided sample file called studentdata.txt. It contains one line for each student...
On Python Preview the provided sample file called studentdata.txt. It contains one line for each student in an imaginary class. The student’s name is the first thing on each line, followed by some exam scores. The number of scores might be different for each student. Using the text file studentdata.txt write a program that calculates the average grade for each student, and print out the student’s name along with their average grade with two decimal places.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
The file Hotel Prices contains the prices in British pounds (about US$ 1.52 as of July...
The file Hotel Prices contains the prices in British pounds (about US$ 1.52 as of July 2013) of a room at two-star, three-star, and four-star hotels in cities around the world in 2013. City Two-Star Three-Star Four-Star Amsterdam 74 88 116 Bangkok 23 35 72 Barcelona 65 90 106 Beijing 35 50 79 Berlin 63 58 76 Boston 102 132 179 Brussels 66 85 98 Cancun 42 85 205 Chicago 66 115 142 Dubai 84 67 111 Dublin 48 66...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT