Question

I am new to Python and am having trouble coming up with writing code to the following problem...

The program must:

  1. Prompt for a file name
  2. Opens that file and reads through the file
  3. Displays a custom error message if the file does not exist
  4. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon.
    From [email protected] Sat Jan 5 09:14:16 2008
  5. Accumulated the counts for each hour (key)
  6. Print out the count for each of the hours after the program has completed execution. The counts should be sorted by hour as shown below.
    00 13
    01 17
    02 01
    03 29
  7. You can download the data at mbox.txtPreview the documentView in a new window when you are testing enter mbox.txt as the file name.
  8. Following execution of the program, begin the quiz for this assignment Python Data Structures.
    • You will be asked to enter the counts for three of the hours your application displays (a portion of the grade for this assignment will be to get the counts correct).
    • You can take the quiz again if you get the counts incorrect.
  9. You must upload your final code as a TEXT file in the question at the bottom of the quiz to receive points for the code. Make sure the filename includes your name
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Array for storing hours count
hoursCount=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
filename=input("Enter file name:")
#handling file not found exception
try:
   f=open(filename,"r")
   #reading first line from text file
   line=f.readline()
   #iterating through all lines
   while(line):
       #If the line start with string From then we split the line to get hour value
       if line.startswith('From'):
           time=line.split()[5]
           hour=time.split(':')[0]
           #increasing count in the hoursCount array
           hoursCount[int(hour)]=hoursCount[int(hour)]+1
       line=f.readline()
   #printing the hours count in sorted order
   for i in range(0,24):
       if hoursCount[i]!=0:
           print(str(i).zfill(2)+" "+str(hoursCount[i]))
   f.close()
except:
   print("File not found")
#Please do upvote if you liked it


Add a comment
Know the answer?
Add Answer to:
I am new to Python and am having trouble coming up with writing code to the...
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
  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • Write a program that counts the distribution of the hour of the day for all the...

    Write a program that counts the distribution of the hour of the day for all the event logs in the messages file. You can pull the hour from the “Nov, Oct, etc. ” line by finding the time string and then splitting that string into parts using the colon character. Once you have accumulated the counts for each hour, print out the counts, one per line, sorted by hour as shown below.   Sample Execution: python timeofday.py Enter a file name:...

  • I am writing python code. I submitted an assignment but the professor said it was a...

    I am writing python code. I submitted an assignment but the professor said it was a modularization. Can you help me see what part of the code is modularization? Pseudocode: 1. Decalre MainMethod () function: # A. Parameters: Three numbers # B. Put the three numbers in reverse # 2. Main Program # A. Initialize Varibles # B. Accepts three values from the user # C. Invoke MainMethod () function, passing the three numbers as arguments in reverse # D....

  • Could anyone please help with this Python code assignment? In this programming assignment you are to...

    Could anyone please help with this Python code assignment? In this programming assignment you are to create a program called numstat.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 count of how many numbers are in the file. The average of the numbers. The average is the sum of the numbers divided by how many there are. The maximum value. The...

  • I am having problems with reading a file into an array. This is my code. This...

    I am having problems with reading a file into an array. This is my code. This is what I get when I run my program. But this is my text file I am reading. I tried everything and it seems to be reading in the last digit of the file. I want to read in their names line by line into an array and ultimatly also read in the scores line by line. 1 E/7 Programming Assignment 6.cpp Defines the...

  • I am working on Exercise 5.8 from Fundamentals of Python 2nd edition. Do you have a...

    I am working on Exercise 5.8 from Fundamentals of Python 2nd edition. Do you have a solution for this item? Thank you. Here is the code I have. I just need 1 instance of each to show in my output. should look like: AM 3 I 3 SAM 3 Thank you... # Put your code here #fileName = input("Enter the file name: ") fileName = "example.txt" #fileName = "kgtest.txt" inputFile = open(fileName, 'r') text = inputFile.read() #print(text) words = text.split()...

  • This needs to be in python, however, I am having trouble with the code. Is there...

    This needs to be in python, however, I am having trouble with the code. Is there any way to use the code that I already have under the main method? If so, what would be the rest of the code to finish it? #Create main method def main(): #Create empty list list=[] #Display to screen print("Please enter a 3 x 4 array:") #Create loop for rows and have each entered number added to list for i in range(3): list.append([int(x) for...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • python question! points) Attached to the Exam #4 code Assignment in Tracs you ## will find...

    python question! points) Attached to the Exam #4 code Assignment in Tracs you ## will find a file, "text.txt". Text is stored 1 sentence per line. ## Write a program below that reads the file contents and displays ## the following: ## 1. Number of uppercase characters in the file ## 2. Number of lowercase characters in the file ## 3. Number of digits in the file. ## 4. Number of whitespace characters in the file ## ## print("Number of...

  • 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...

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