ProductMaster = { 1 : [Minor Widget,0.25,250]
2 : [Critical Widget,5.00,10]
3 : [Complete System (Basic),500,1]
4 : [Complete System (Deluxe),625,1]
}
I got the dictionary (ProductMaster) above by reading a csv file and then I did some calculation to make the dictionary (productReport) below.
productReport = { 1 : [687500.0,11000,0.0]
2 : [250000.0,5000,12500.0]
3 : [1500000.0,3000,92500.0]
4 : [0,0,0]
}
The first index of the list value in dictionary (productReport) above represents gross revenue, and I want to output a csv file using the dictionary above in descending order in terms of gross revenue.
with open(ProductReport_Filename, 'w') as file:
csvWriter = csv.writer(file)
csvWriter.writerow(["Name","GrossRevenue","TotalUnits","DiscountCost"])
productKeys = ProductMaster.keys()
for key,value in productReport.items():
if key in productKeys:
csvWriter.writerow([ProductMaster[key][0], *value])
file.close()
The code above gives the csv file below:
Name,GrossRevenue,TotalUnits,DiscountCost
Minor Widget,687500.0,11000,0.0
Critical Widget,250000.0,5000,12500.0
Complete System (Basic),1500000.0,3000,92500.0
Complete System (Deluxe),0,0,0
But I want the csv file to be sorted by gross revenue in descending order. So, the output should be:
Name,GrossRevenue,TotalUnits,DiscountCost
Complete System (Basic),1500000.0,3000,92500.0
Minor Widget,687500.0,11000,0.0
Critical Widget,250000.0,5000,12500.0
Complete System (Deluxe),0,0,0