Skip to content

Instantly share code, notes, and snippets.

@tef
Created February 28, 2018 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tef/600a0b7310373e2fee8118cbd71513ad to your computer and use it in GitHub Desktop.
Save tef/600a0b7310373e2fee8118cbd71513ad to your computer and use it in GitHub Desktop.
import csv
import collections
import datetime
dates = {}
with open('tweets.csv') as fh:
fh.readline()
reader = csv.reader(fh)
for row in reader:
date,time,zone = row[3].split(' ')
if date < "2012":
continue
y,m,d = date.split('-')
date = datetime.datetime(int(y),int(m), int(d))
if date not in dates:
dates[date] = collections.Counter()
hour = int(time[:2])
dates[date][hour]+=1
oldest,newest = min(dates.keys()), max(dates.keys())
for d in range((newest-oldest).days):
day = oldest + datetime.timedelta(days=d)
if day not in dates:
dates[day] = {}
for date in sorted(dates):
day = ["_"] * 48
for hour, count in dates[date].iteritems():
day[hour] = "*"
next = date + datetime.timedelta(days=1)
if next in dates:
for hour, count in dates[next].iteritems():
day[hour+24] = "*"
print('"{:04}-{:02}-{:02}",{}'.format(date.year,date.month,date.day, ",".join(day)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment