Created
February 28, 2018 16:39
-
-
Save tef/600a0b7310373e2fee8118cbd71513ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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