Created
February 22, 2012 00:28
-
-
Save yosemitebandit/1880170 to your computer and use it in GitHub Desktop.
pull timeseries data from a csv and export a js file with formatting suitable to flot
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
#!/usr/bin/env python | |
''' | |
csv_to_flot_data.py | |
extract data from a csv; save a js file with flot-compatible data | |
usage: | |
$ python csv_to_flot_data.py /path/to/input.csv /path/to/output.js | |
''' | |
import csv | |
import sys | |
import time | |
class Extractor(): | |
def __init__(self): | |
self.reader = csv.DictReader(open(sys.argv[1], 'rb')) | |
print self.reader.fieldnames | |
self.time_label = raw_input('\ntype the time label from above: ') | |
if self.time_label not in self.reader.fieldnames: | |
print 'not recognized' | |
sys.exit(0) | |
self.data_label = raw_input('type the data label from above: ') | |
if self.data_label not in self.reader.fieldnames: | |
print 'not recognized' | |
sys.exit(0) | |
self.output_filename = sys.argv[2] | |
def process(self): | |
self.data = [] | |
for row in self.reader: | |
# two possible date formats | |
try: | |
formatted_time = time.mktime(time.strptime( | |
row[self.time_label], '%Y-%m-%d')) | |
except ValueError: | |
formatted_time = time.mktime(time.strptime( | |
row[self.time_label], '%d-%m-%Y')) | |
# convert times to ms | |
self.data.append([formatted_time*1000 | |
, float(row[self.data_label])]) | |
def save(self): | |
f = open(self.output_filename, 'w') | |
f.write('var d = %s;' % str(self.data)) | |
f.close() | |
print 'tada!' | |
if __name__ == '__main__': | |
extractor = Extractor() | |
extractor.process() | |
extractor.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment