json - aggregate and sum daily data to month in python -


I have a two-dimensional list in Python, which contains era seconds and related values ​​for the day. There is a need to collect a Jason array of months and the sum of all related daily values.

Python inventory looks like this:

  array = [[1230768000000, 23], [1244073599000, 5], [1269206974000, 8], [1291908086000, 23] ...   

I need jsonify in a Jason array that looks like this:

  [{key: '2009-01', Value: 28}, {key: '2009-02', value: 324} ...]   

I have tried the following code but it really does not really want me to be desired. [D, v] in array: truncated = int (str (d) [: - 3]) year_month = datetime.utcfromtimestamp (truncated) .date for

  month_aggregate = defaultdict (list). (). Isoformat () [: - 3] month_one [year_m]] .append (v)> gt; & Gt; {'2011-08': [55 9, 601, 545, 578], '2011-09': [572, 491, 595], ...}   

Tips very much appreciated

Try this:

  array = [[1230768000000, 23] ], [1244073599000, 5], [12 9 9 74000, 8], [12 9, 908000, 23]] for month_one = dict () in [d, v] array: truncated = int ( Str (d) [: - 3]) year_month = datetime UTC framestamp (deduction) .date (). Iofformat () [: - 3] # If the entry was not present first, make one with the current value, if not month_aggregate.has_key (year_month): month_grid [year_month] = v further: # Otherwise add the value to the previous entry month_aggregate [ V ',' key ':}] {{' key ':' 2009-01 ',' value 'in the month (month)] = = v # month_aggregate dictionary month_aggregate_json_list = [vs. JSON Array], in month_grid.itts (v) : '23', {'key']   

print

'2009-06', 'value': 5}, {'key': '2010-03', 'value': 8}, {'Y': '2010-12', 'value': 23}]

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -