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

Output:-

Thanking you.
Write a program to Read and parse the “Nov ” lines and pull out the event...
9.4 Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using...
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 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:...
For part one of this assignment, write a program (parse.c) that contains a function to parse a single line of input and and prints out the individual tokens. Part 1 - Single Line Parser For part one of this assignment, you will need to learn how to do some parsing (or more accurately--lexing a line, you do not have to check for correctness). You have previously done some parsing with the cycle count tool perhaps using functions such as strcmp....
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 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 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 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 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...