[Chicago] 2 unique keys in a python dict?
rcriii at ramsdells.net
rcriii at ramsdells.net
Thu Oct 9 22:06:37 CEST 2008
Can I try? If you have a csv file as below, the csv module will return an
iterable where each item is a list representing the line of data. So if
you have a csv file that looks like this:
id,subid, paid,transaction
3,1,$10,20080101
3,2,$10,20080201
4,1,$20,20080922
42,1,$24,20080923
>>> import csv
>>> f = open('somedata.csv','r')
>>> r = csv.reader(f)
>>> data = [x for x in r]
>>> data
[['id', 'subid', ' paid', 'transaction'], ['3', '1', '$10', '20080101'],
['3', '2', '$10', '20080201'], ['4', '1', '$20', '20080922'], ['42', '1',
'$24', '20080923']]
>>> sum([int(x[2][1:]) for x in data if x[0]=='3'])
20
>>> max([int(x[3]) for x in data if x[0]=='3'])
20080201
Robert
>
>
> I have multiple records in a csv file for each userid. I need to
> calculate total amount paid and last transaction.
>
> id,paid,transaction
> 3,$10,20080101
> 3,$10,20080201
>
>
> Final Total
> me={}
> me[3]=($20,20080201) (dictionary with a list)
>
> now
> id,subid, paid,transaction
> 3,1,$10,20080101
> 3,2,$10,20080201
>
> I guess what Massimo said will work, but now I need to know which row
> is the paid and which is transaction?!!
> me[3,1]=(10,20080101)
> me[3,2]=(10,20080201)
>
> I guess this could do it
> me[3,1]={'paid':10 , 'transaction':20080101}
>
> Thanks,
> Lucas
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
More information about the Chicago
mailing list