Skip to content

Instantly share code, notes, and snippets.

@claymcleod
Created September 1, 2014 23:42
Show Gist options
  • Save claymcleod/9a45a6666dd32d682269 to your computer and use it in GitHub Desktop.
Save claymcleod/9a45a6666dd32d682269 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os
import csv
import math
import datetime
from optparse import OptionParser
TIME_FORMAT = '%Y,%m,%d,%H,%M,%S'
TIME_HEADERS = 'year,month,day,hour,minute,second'
parser = OptionParser()
parser.add_option("-d", "--dispose",
action="store_true", dest="dispose_utc", default=False,
help="will dispose of the UTC column");
parser.add_option("-f", "--file",
action="store", type="string", dest="fileName", default="results.csv",
help="specify the csv file you would like to process");
(options, args) = parser.parse_args()
indexOfUTC = -1; # assume non-existant
rowsOfFile = [] # start empty
fileName = options.fileName # simply the file name to be read and written to
if(os.path.exists(fileName)):
with open(fileName, 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
i = 0
for row in spamreader:
row_to_add = []
if len(row) <= 0 or not ',' in row[0]:
continue
array = row[0].split(',')
for val in array:
val.strip()
row_to_add.append(val)
if i == 0:
# Headers
if "created_utc" in array:
indexOfUTC = array.index("created_utc")
else:
raise RuntimeError('Invalid headers: \'created_utc\' column not found!')
for header in TIME_HEADERS.split(','):
header.strip()
row_to_add.append(header)
else:
# Regular data lines
utc = datetime.datetime.utcfromtimestamp(int(math.floor(float(array[indexOfUTC]))))
time_data = utc.strftime(TIME_FORMAT)
for val in time_data.split(','):
val.strip()
row_to_add.append(val)
rowsOfFile.append(row_to_add)
i = i + 1
csvfile = open(fileName.replace('.csv', '_converted.csv'), 'wb')
spamwriter = csv.writer(csvfile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in rowsOfFile:
spamwriter.writerow(row)
csvfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment