Question

Write a program to Read and parse the “Nov ” lines and pull out the event...

Write a program to Read and parse the “Nov ” lines and pull out the event message from the line. Count the number of messages from "kernel" using a dictionary.

After all the data has been read, print the date with the most occurrence by creating a list of (count, date) tuples from the dictionary and then sorting the list in reverse order and print out the date which has the most occurrence.

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

Solution:-

Code to copy:-

kernel_count = {"kernel":0} #Dictionary for kernal count

date_count = {} #Dictionary for date count

with open ("messages.2","r") as f: #OPen file

for line in f:

if line.strip().split()[0] == "Nov": #Parse lines starting with Nov

try:

if line.strip().split()[4] == "kernel:": #Check for message from kernel

kernel_count['kernel'] +=1 #Update dict

except:

continue

date = line.strip().split()[1] #get date from line

date_count[date] = date_count.get(date, 0) + 1 #update dict counter

li = [(x,y) for x,y in date_count.items()] #Convert dict to list

s_li = sorted(li, key = lambda x: x[1], reverse=True) #Sort in decending order

print(s_li)

print("Number of messages from 'kernal':",kernel_count['kernel'])

print("Most occuring date:",s_li[0][0])

Screen shot:-

kernel_count = {kernel:0} #Dictionary for kernal count date_count = {} #Dictionary for date count 3 with open (messages.2

Output:-

Number of messages from kernal 70 Most occuring date: 7

Thanking you.

Add a comment
Know the answer?
Add Answer to:
Write a program to Read and parse the “Nov ” lines and pull out the event...
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
  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

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

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Round 1: sequence.c This program should read and execute a list of commands from stdin. Each comm...

    Round 1: sequence.c This program should read and execute a list of commands from stdin. Each command and its arguments (if any) will appear on a separate line. For example, if the file "cmdfile" contains the lines: whoami cal 4 2019 echo The time is: date then running 1 /sequence< cmdfile should output your username, a calendar of the month of April, the string "The time is:", and the current date/time, to standard output. Suggested approach: first, make sure you...

  • Prelab Exercises Your task is to write a Java program that will print out the following...

    Prelab Exercises Your task is to write a Java program that will print out the following message (including the row of equal marks): Computer Science, Yes!!!! ========================= An outline of the program is below. Complete it as follows: a. In the documentation at the top, fill in the name of the file the program would be saved in and a brief description of what the program does. b. Add the code for the main method to do the printing. //...

  • C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

    C Programming Language on Linux - Word Frequency Program Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following: First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0. Then, read the file (second pass) and for each word identified, search the linked list, and when found,...

  • (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

    (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and...

  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

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