1

I've written code of function which is used to convert csv file to dictionary, output of code is given below.....

INPUT

    input_file_obj = open(file_name, 'r')
    read_csv = csv.reader(input_file_obj)
    next(read_csv)

    num_list = []
    mark_dict = {}
    num = 1

    for row in read_csv:
        
        while  num <= 5:
            num_list.append(row[num])
            num += 1

        mark_dict['marks'] = num_list

        name_marks_mapping[row[0]] = mark_dict
    
    input_file_obj.close()

    return name_marks_mapping

OUTPUT

   {'Artus Syne': {'marks': ['43', '71', '55', '16', '51']}, 'Evey Reburn': 
   {'marks': ['43', '71', '55', '16', '51']}, 'Giff Wickmann': {'marks': ['43', 
   '71', '55', '16', '51']}, 'Garrot Casetta': {'marks': ['43', '71', '55', '16', 
   '51']}, 'Roselle Maes': {'marks': ['43', '71', '55', '16', '51']}, 'Torin 
   Ziehms': {'marks': ['43', '71', '55', '16', '51']}, 'Jaye Etock': {'marks': 
   ['43', '71', '55', '16', '51']}, 'Thomasina Tinkham': {'marks': ['43', '71', 
   '55', '16', '51']}, 'Adolphus Biernat': {'marks': ['43', '71', '55', '16', 
   '51']}, 'Rex Aspinell': {'marks': ['43', '71', '55', '16', '51']}}

I wanna make this output looking better like is given in expected output. Can some tell me how can I do this?

EXPECTED OUTPUT

   {
    'Artus Syne': {'marks': [43.0, 71.0, 55.0, 16.0, 51.0]},
    'Evey Reburn': {'marks': [49.0, 7.0, 53.0, 50.0, 63.0]},
    'Giff Wickmann': {'marks': [63.0, 37.0, 21.0, 87.0, 9.0]},
    'Garrot Casetta': {'marks': [22.0, 3.0, 91.0, 75.0, 52.0]},
    'Roselle Maes': {'marks': [71.0, 90.0, 96.0, 79.0, 48.0]},
    'Torin Ziehms': {'marks': [71.0, 31.0, 83.0, 1.0, 25.0]},
    'Jaye Etock': {'marks': [92.0, 9.0, 2.0, 78.0, 55.0]},
    'Thomasina Tinkham': {'marks': [25.0, 78.0, 46.0, 46.0, 90.0]},
    'Adolphus Biernat': {'marks': [91.0, 96.0, 98.0, 94.0, 100.0]},
    'Rex Aspinell': {'marks': [34.0, 75.0, 51.0, 38.0, 99.0]}
   }

2 Answers2

2

Not exactly, but close: you can use pprint (d is your dictionary from the question):

from pprint import pprint

pprint(d)

Prints:

{'Adolphus Biernat': {'marks': ['43', '71', '55', '16', '51']},
 'Artus Syne': {'marks': ['43', '71', '55', '16', '51']},
 'Evey Reburn': {'marks': ['43', '71', '55', '16', '51']},
 'Garrot Casetta': {'marks': ['43', '71', '55', '16', '51']},
 'Giff Wickmann': {'marks': ['43', '71', '55', '16', '51']},
 'Jaye Etock': {'marks': ['43', '71', '55', '16', '51']},
 'Rex Aspinell': {'marks': ['43', '71', '55', '16', '51']},
 'Roselle Maes': {'marks': ['43', '71', '55', '16', '51']},
 'Thomasina Tinkham': {'marks': ['43', '71', '55', '16', '51']},
 'Torin Ziehms': {'marks': ['43', '71', '55', '16', '51']}}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

Say you have:

my_dict = {"a" : [1,2,3], "b" : [4,5,6], "c" : [7,8,9]}

print(my_dict) will print:

{'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}

While

print("{" + "".join(["\n '%s\' : %s" % (k, str(my_dict[k])) for k in my_dict]) + "\n}")

will print:

{
 'a' : [1, 2, 3]
 'b' : [4, 5, 6]
 'c' : [7, 8, 9]
}
logi-kal
  • 7,107
  • 6
  • 31
  • 43