filename=input("Enter the file name: ") #getting the file name
if len(filename) < 1 : filename="mbox-short.txt"
fh=open(filename) #opening the file
rline={} #making a dictionary
for line in fh:
if line.startswith("From "): #getting the hours from the word 'From'
hours=line.split()[5] #getting only time starting from position 1
time=hours.split(":")[0] #getting only hours starting from position 1
rline[time]=rline.get(time,0) + 1 #logic for dictionary counting
lst=list(rline.items()) #converting the dictionary into list
lst.sort() #sorting the list
for a in lst:
print(a[0], a[1]) #looping to get the vertical number arrangements
Post a Comment