I would like to improve my code's readability and formatting. I have this code, that works, but I feel like it could be tighter than this, I just can't seem to get it to work any other way. The idea is to read a .txt file find incoming e-mail strings and organize the data by frequency of hour sent.
Here is an example line that I'm looking for in the text:
From [email protected] Sat Jan 5 09:14:16 2008
Here is my code as it is today.
fname = input("Enter file:")
if len(fname) <1 : fname = "mbox-short.txt"
fh = open(fname)
time = list()
hours = list()
hr = dict()
for line in fh:
if not line.startswith("From "): continue
words = line.split()
time.append(words[5])
for i in time:
i.split(":")
hours.append(i[:2])
for h in hours:
hr[h]=hr.get(h, 0)+1
l = list()
for k,v in hr.items():
l.append((k,v))
l.sort()
for k, v in l:
print (k,v)
Just some hint : (Don't try this at home, it's really bad code :D, but show some python structs to learn) (operator, defaultdict and list comprehension)