-3

I have multiple dictionaries like this, in which the keys are the age, and the values are the number of the persons with that age. And I grouped them into the multiple dictionaries

    age_50 = {39: 66, 40: 83, 42: 217, 43: 53, 23: 33, 21: 28} 
    age50_80 = {59: 397, 65: 408, 61: 120, 67: 241, 68: 372, 78: 424, 76: 635 } 
    age_80 = {80: 93, 88: 425, 83: 165, 84: 58, 91: 88} 

I want to write it into a CSV file in a way that each column is a dictionary and the rows are the value for each key. I want my output would be the below table. If the key exists, write the value. If not just fill with 0:

Age_Under50    Between_50_80        Greater_80
66              397                   0
83              408                  93
217             120                 425
53              241                   0
33              372                 165
28              424                  58
0               635                  88
a.mah
  • 3
  • 4
  • ...If the key exists, write the value. If not just fill with 0... What do you mean if the key exists? Which keys are you expecting to exist? – Krishna Dec 01 '18 at 05:36
  • for example: if key in Age_Under50: Age_Under50.get(key, 0) – a.mah Dec 01 '18 at 05:41
  • age_50 has 6 keys in total, so I assume you will get 44 0s in the first column. Am I correct? Also what is the range for age_80. 80-? – Krishna Dec 01 '18 at 05:44
  • yes, that's correct for 44 since it doesn't exist 0s. for "age_80" >= 80 – a.mah Dec 01 '18 at 05:47

1 Answers1

1

Try something like below

import csv, itertools

age_50 = {39: 66, 40: 83, 42: 217, 43: 53, 23: 33, 21: 28} 
age50_80 = {59: 397, 65: 408, 61: 120, 67: 241, 68: 372, 78: 424, 76: 635 } 
age_80 = {80: 93, 88: 425, 83: 165, 84: 58, 91: 88} 

cf = csv.writer(open("test.csv", "w"))

cf.writerows(
    itertools.zip_longest(
        ['Age_Under50']     + [age_50.get(k, 0)   for k in range(50)],
        ['Between_50_80']   + [age50_80.get(k, 0) for k in range(50,80)],
        ['Greater_80']      + [age_80.get(k, 0)   for k in range(80,100)],
        fillvalue=0
    )
)

For windows

import csv, itertools

age_50 = {39: 66, 40: 83, 42: 217, 43: 53, 23: 33, 21: 28} 
age50_80 = {59: 397, 65: 408, 61: 120, 67: 241, 68: 372, 78: 424, 76: 635 } 
age_80 = {80: 93, 88: 425, 83: 165, 84: 58, 91: 88} 

f = open("test.csv", "w", newline='')
cf = csv.writer(f)

cf.writerows(
    itertools.zip_longest(
        ['Age_Under50'] + [age_50[k] for k in age_50],
        ['Between_50_80']+[age50_80[k] for k in age50_80],
        ['Greater_80']+[age_80[k] for k in age_80],
        fillvalue=0
    )
)

f.close()
Krishna
  • 924
  • 1
  • 7
  • 28
  • It works, but I change the code a little, there is an issue though with the code I changed. It creates an extra row per each iteration. cf.writerows( itertools.zip_longest( ['Age_Under50']+ [age_50[k] for k in age_50], ['Between_50_80']+[age50_80[k] for k in age50_80], ['Greater_80']+[age_80[k] for k in age_80], fillvalue=0)) – a.mah Dec 01 '18 at 06:39
  • for sure. Just run the code I changed. it creates an extra line. would you tell me how to solve it? – a.mah Dec 01 '18 at 07:01
  • It works fine for me, works here with no extra zeros http://tpcg.io/IkjVaT. Python3 !!! – Krishna Dec 01 '18 at 07:10
  • for the link you just gave, it's not creating an extra line. But remember in the code you have not used (cf.writerows). Please run the last code, and open the csv file. you'll see what I'm talking about. – a.mah Dec 01 '18 at 07:20
  • Updated to read back the file http://tpcg.io/DkISCV. now it shows all chars. There is only one '\n' per line. – Krishna Dec 01 '18 at 07:25
  • No, Still creates an extra line. Since I want to deliver the CSV file, I just want it to look good. would check this link on StackOverflow. https://stackoverflow.com/questions/8746908/why-does-csv-file-contain-a-blank-line-in-between-each-data-line-when-outputting – a.mah Dec 01 '18 at 07:37
  • 1
    I fixed it. here's how. f = open("test.csv", "w", newline = "") – a.mah Dec 01 '18 at 07:48
  • Yeah just figured out the fix for windows. Thank for it. – Krishna Dec 01 '18 at 07:51