Example #1
0
def save2csv(r, csvpath=None, fileNamePrefix=''):
    """
    Parse and save to csv
    """

    # Create Dataframe
    try:
        d = {}
        for tup in r.json():
            d[dt.fromtimestamp(tup[0])] = tup[1]
        #pdb.set_trace()
        Ts = TimeSeries(data=d)
        # this line gives an error.  Should be checked, but for now I keep the nan's
        # Ts = Ts[Ts != 'nan']

    except:
        print "-------> Problem with Flukso data parsing <-------"
        raise

    # save to file
    if csvpath is None:
        csvpath = os.getcwd()
    s = strftime("%Y-%m-%d_%H-%M-%S", Ts.index[0].timetuple())
    e = strftime("%Y-%m-%d_%H-%M-%S", Ts.index[-1].timetuple())
    Ts.to_csv(
        os.path.join(csvpath,
                     fileNamePrefix + '_FROM_' + s + '_TO_' + e + '.csv'))