-
-
Save zcakzwa/2bf34f782db0c43847250b5fa86bc2bf to your computer and use it in GitHub Desktop.
| name = raw_input("Enter file:") | |
| if len(name) < 1 : name = "mbox-short.txt" | |
| handle = open(name) | |
| d=dict() | |
| for line in handle: | |
| if not line.startswith("From "): | |
| continue | |
| else: | |
| line=line.split() | |
| line=line[5] | |
| line=line[0:2] | |
| d[line]=d.get(line,0)+1 | |
| lst=list() | |
| for value,count in d.items(): | |
| lst.append((value,count)) | |
| lst.sort() | |
| for value,count in lst: | |
| print value,count |
Inspire2023
commented
Mar 15, 2024
Hey guys this is my code on the Q it works nicely but if there is anything that I did wrong or I could have initially avoided pls let me know :)
fname = input("Enter File Name: ")
fhandle = open(fname)
lst = list()
count = dict()
finallist = list()
for line in fhandle:
if line.startswith("From") and not line.startswith("From:"):
line = line.split()
lst.append(line[5])
for hour in lst:
hour = hour.split(":")
finallist.append(hour[0])
for ftime in finallist:
count[ftime] = count.get(ftime, 0) + 1
for x, y in sorted(count.items()):
print(x, y)
name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
counts = dict()
for line in handle:
line = line.strip()
if line.startswith('From '):
word = line.split()
time = word[5]
hour = time.split(':')[0]
counts[hour] = counts.get(hour,0) + 1
for k,v in sorted(counts.items()):
print(k,v)