python - file to dictionary:value is a dictionary where the key is repeated -
I open a file containing:
No transaction customer customer purchase Purchase ---- --------- ---- ---------- ------------- 1 09-04-2014 Barco Bama Potato: 2.67, sugar: 1.98, cereals: 5.99 crisps: 1.0 9 2 11-04-2014 Barcoba parsley: 0.76 grains: 3.22 3 11-04-2014 vladimirputin bread: 0.66, milk: 2.87, parsley: 1.33 > and I would like the output dictionary like this:
{'milk': {'vladimirputin': 2.87}, 'grain': {'barcomba': 9.21 }, 'Roti': {'' '' potato ': {' barcomba ': 2.67},' sugar ': {' barcomba ': 1 9 8}, 'parsley': {'vladimirputin': 1.33, I did this: for the line in the file
C = {} File = open (filename name, 'r'): if the line [0] .isdigit (): fields = line partition ()
c = {} f = open (fileNameStr): if not line [0] .isdigit (): continue the transaction, _date, customerId , PurchasePairs = line for pairs in purchase .split () pair.Split (','): productName, price = pair.split (':') if productName c: c [productName] [customerId] = float (price ) Else: c [productName]] = {CustomerId: float (price)} You have a relationship Areas to be dealt: a client more than once buys the same time. Hope this code will help.
Comments
Post a Comment