Sep-25-2020, 09:23 PM
Hello there, been learning Python for around 5 days so I am very new and want to form good coding practices/standards early on. I'm sure my code is very messy, badly optomized and not very elegant, just some pointers in the right direction would be amazing. Some comments within my code are likely not needed, they were mostly for my own learning/remembering. I purposely haven't investigated any Python libararies that can do what my code does as for now I want to get a solid grasp of the fundamentals and basics before exploring libraries etc. I'm highly dedicated and spending a minimum of 3 hours each day to learning, I'm likely to increase this to 5-6 hours per day as I learn where to go next/start another course.
Code does the following:
- Takes a long list of emails within text.
- You need to only parse and gather emails that are in lines that start with 'From '.
- Tells you the top 5 most common hours, days, months and emails that occurs within the text.
- Prints them out in a very ugly way (need to make it much better).
Text can be found here:
http://www.py4inf.com/code/mbox-short.txt
My code which needs a lot of work but it's my first ever attempt at something.
Code does the following:
- Takes a long list of emails within text.
- You need to only parse and gather emails that are in lines that start with 'From '.
- Tells you the top 5 most common hours, days, months and emails that occurs within the text.
- Prints them out in a very ugly way (need to make it much better).
Text can be found here:
http://www.py4inf.com/code/mbox-short.txt
My code which needs a lot of work but it's my first ever attempt at something.
# Parse a list of emails, find most common hours, days, months and emails. filename = input("Enter filename: ") try : filehandle = open(filename) except : print("Unable to open file:", filename) quit() # Take a list and a dictionary, store a list of tuples, swap key / value, reverse order. def dict_to_list(list, diction) : for key, value in diction.items() : temptup = (value, key) list.append(temptup) list = sorted(list, reverse=True) return list hours = dict() days = dict() months = dict() emails = dict() # Go through each line, extract relevant data, store in dicts. for line in filehandle : if line.startswith("From ") : words = line.split() email = words[1] emails[email] = emails.get(email, 0) + 1 day = words[2] days[day] = days.get(day, 0) + 1 month = words[3] months[month] = months.get(month, 0) + 1 for word in words : if word.find(":") > 1 : time = word.split(":") hour = time[0] hours[hour] = hours.get(hour, 0) + 1 # Create sorted lists of tuples using custom dict_to_list function. listhours = list() hourslist = dict_to_list(listhours, hours) listdays = list() dayslist = dict_to_list(listdays, days) listmonths = list() monthslist = dict_to_list(listmonths, months) listemails = list() emailslist = dict_to_list(listemails, emails) # Print the top 5 most common from each list. for k, v in hourslist[:5] : print("Most common hour:", v, "occurs", k, "times.") for k, v in dayslist[:5] : print("Most common day:", v, "occurs", k, "times.") for k, v in monthslist[:5] : print("Most common month:", v, "occurs", k, "times.") for k, v in emailslist[:5] : print("Most common email:", v, "occurs", k, "times.")