Created
November 24, 2014 22:20
-
-
Save jjjake/2f1bb5e4835ca0e86830 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import sys | |
import csv | |
from datetime import datetime | |
from dateutil import tz | |
def get_est(date): | |
from_zone = tz.gettz('UTC') | |
to_zone = tz.gettz('EST') | |
utc = datetime.strptime(date, '%Y%m%d_%H%M%S') | |
# Tell the datetime object that it's in UTC time zone since | |
# datetime objects are 'naive' by default | |
utc = utc.replace(tzinfo=from_zone) | |
est = utc.astimezone(to_zone) | |
est_date = est.strftime('%Y%m%d_%H%M%S') | |
return est_date | |
if __name__ == '__main__': | |
input_file = csv.DictReader(open(sys.argv[-1])) | |
output_file = '{}-est.csv'.format(sys.argv[-1].split('/')[-1].replace('.csv', '')) | |
with open(output_file, 'wb') as fp: | |
# IA_Philly_Media_Watch_IDENTIFIED_ADS_v1.0.csv | |
#fields = [ | |
# 'ad identifier (state, year, race, sponsor, candidate, seconds pro, seconds con)', | |
# 'url', | |
# 'candidate', | |
# 'sponsor', | |
# 'network', | |
# 'date_time (UTC)', | |
# 'date_time (EST)', | |
# 'seconds\r\ninto program', | |
# 'identifier', | |
#] | |
# IA_Philly_Media_Watch_PROGRAMS_v1.0.csv | |
fields = [ | |
'# ads /\r\nprogram', | |
'program title', | |
'network', | |
'date_time (UTC)', | |
'date_time (EST)', | |
'program identifier', | |
'length of program\r\n/seconds', | |
'length of program\r\n/minutes', | |
] | |
w = csv.DictWriter(fp, fields) | |
w.writeheader() | |
for row in input_file: | |
try: | |
est = get_est(row['date_time (UTC)']) | |
except: | |
est = None | |
row['date_time (EST)'] = est | |
w.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment