Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mailbox
- from email.utils import parsedate
- from dateutil.parser import parse
- import itertools
- import plotly.plotly as py
- from plotly.graph_objs import *
- import plotly.tools as tls
- import csv
- from itertools import izip
- path = 'mail.mbox'
- mbox = mailbox.mbox(path)
- def extract_date(email):
- date = email.get('Date')
- return parsedate(date)
- #sort the email by a given date
- sorted_mails = sorted(mbox, key=extract_date)
- mbox.update(enumerate(sorted_mails))
- mbox.flush()
- #it finds all the dates within the MBOX and split
- all_dates = []
- mbox = mailbox.mbox(path)
- for message in mbox:
- all_dates.append( str( parse( message['date'] ) ).split(' ')[0] )
- #counts the number of emails per given date
- email_count = [(g[0], len(list(g[1]))) for g in itertools.groupby(all_dates)]
- email_count[0]
- #makes a list of (x,y)
- x = []
- y = []
- for date, count in email_count:
- x.append(date)
- y.append(count)
- #produce a CSV file of X and Y, for plotting
- with open('data.csv', 'wb') as f:
- writer = csv.writer(f)
- writer.writerows(izip(x, y))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement