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

​​​​​​​LANGUAGE IS JAVA Part One A hotel salesperson enters sales in a text file. Each line...
​​​​​​​LANGUAGE IS JAVA Part One 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. Prompt the user for data to write the file. Part Two Write a program that reads the text file as described above, and that writes a separate file for each service...
how to creat Create a text file that contains a list of integer numbers separated by...
how to creat Create a text file that contains a list of integer numbers separated by comma (numbers.txt). E.g. 3, 7, 5, 1, 11, 8,2, 6 2- how to Write a python program (minMax.py) to read the file numbers.txt and find the largest and smallest numbers in the file. (without using use min(), max()). calculate the average and output the results. 3-Use python file i/o, write a python program (fileCopy.py) that makes a copy of minMax.py (Read contents of minMax.cp...
The input file Each line of the input file will contain a sentence with words separated...
The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line. The input file . Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go. Read the above that contains a paragraph of words. Put all the words in an array, put the...
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,...
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
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...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT