jeudi 13 octobre 2016

adding data from different rows in a csv belonging to a common variable

this is my csv excel file information:

  Receipt merchant    Address      Date    Time    Total price
    25007   A      ABC pte ltd   3/7/2016   10:40   12.30
    25008   A      ABC ptd ltd   3/7/2016   11.30   6.70
    25009   B      CCC ptd ltd   4/7/2016   07.35   23.40
    25010   A      ABC pte ltd   4/7/2016   12:40   9.90

how is it possible to add the 'Total Price' of each line together only if they belong to the same 'merchant', 'date' and 'time' then grouping them together in a list or dict, example: {['A','3/7/2016', '19.0'], ['A',4/7/2016, '9.90'],..} My previous code does what i wanted except that i lack the code to count the total price for each same date and merchant.

from collections import defaultdict
from csv import reader
with open("assignment_info.csv") as f:
    next(f) 
    group_dict = defaultdict(list)
    for rec, name, _, dte, time, _,_,_,_ in reader(f):
        group_dict[name, dte].extend(time)
    for v in group_dict.values():v.sort()    

from  pprint import pprint as pp
print 'Sales tracker:'
pp(dict(group_dict))

Aucun commentaire:

Enregistrer un commentaire