I´m working on a Python problem asking me to extract the hour of the day for each of the email messages in a text file and count the number of times they appear.
This is my code so far.
name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
for line in handle:
words = line.split()
if line.startswith("From"):
date = words[5:6]
print(date)
It returns this:
['09:14:16']
[]
['18:10:48']
[]
['16:10:39']
[]
['15:46:24']
[]
['15:03:18']
[]
['14:50:18']
[]
['11:37:30']
[]
['11:35:08']
[]
['11:12:37']
[]
['11:11:52']
[]
['11:11:03']
[]
['11:10:22']
[]
['10:38:42']
[]
['10:17:43']
[]
['10:04:14']
[]
['09:05:31']
[]
['07:02:32']
[]
['06:08:27']
[]
['04:49:08']
[]
['04:33:44']
[]
['04:07:34']
[]
['19:51:21']
[]
['17:18:23']
[]
['17:07:00']
[]
['16:34:40']
[]
['16:29:07']
[]
['16:23:48']
[]
I need to obtain the number before the first colon, butI´m lost about where to begin. Strings are immutable. I´ve tried .rstrip()
as well as running .split(":")
None of these are working. I know I have to utilize key/values in some way because the data needs to go into a dict()…but I´m unsure how.
Any hints would be greatly appreciated. I´m attaching a link to the text file referenced in the code.
Thanks!