Python Forum
how can i create a dictionary of dictionaries from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can i create a dictionary of dictionaries from a file
#1
So i need to create this dictionary(dictionary of dictionaries):

my_dict={'1':{'surname':'Musk', 'name':'Elon', 'gender': 'm','growth':183},
'2':{'surname':'Holmes', 'name':'Enola', 'gender': 'f','growth':175},
'3':{'surname':'Brown', 'name':'Lilly', 'gender': 'f','growth':178},
'4':{'surname':'Lothbrok', 'name':'Ragnar', 'gender': 'm','growth':189},
'5':{'surname':'Smith', 'name':'Adreana', 'gender': 'f','growth':173},
'6':{'surname':'Moon', 'name':'Larry', 'gender': 'm','growth':184}}

I do not understand how I can do this reading and writing from a file to my_dict. And how should I write information in the file.
As a result, I need to find out how to write this information to a file and then how to write it to my_dict.
Reply
#2
the easiest would be to read/write json file. But a csv file would do as well (i.e. using csv.DcitReader and csv.DictWriter.
Astone likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
What is your input data?
Json-Data looks like your example.

For example, this is csv:
1
2
3
4
5
6
7
surname,name,gender,growth
Musk,Elon,m,183
Holmes,Enola,f,175
Brown,Lilly,f,178
Lothbrok,Ragnar,m,189
Smith,Adreana,f,173
Moon,Larry,m,184
Example to open the csv file:
1
2
3
4
5
6
7
import csv
 
rows = []
with open("your_file.csv", encoding="utf8", newline="") as fd:
    for row in csv.DictReader(fd):
        print(row)
        rows.append(row) # adding the dict to rows
Keep in mind that everything from csv-reader is a str.
All decimals and integers are str objects. To use them for sorting or to make calculations with them,
you need to cast the str to int or float.

1
2
3
4
5
6
7
8
import csv
 
rows = []
with open("your_file.csv", encoding="utf8", newline="") as fd:
    for row in csv.DictReader(fd):
        row["growth"] = int(row["growth"]) # str -> int
        print(row)
        rows.append(row) # adding the dict to rows
The other question is: From where are the student numbers (which are also str in your example)?
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 3,625 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Newbie here. Create an array from file data? Rayj00 2 1,936 Jan-13-2023, 01:35 PM
Last Post: perfringo
  Using dictionary to find the most sent emails from a file siliusu 6 9,667 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Updating dictionary in another py file tommy_voet 1 6,472 Mar-28-2021, 07:25 PM
Last Post: buran
  Create a turtle drawing from .txt file Noob101 20 13,586 Jan-29-2021, 04:13 PM
Last Post: nilamo
  create empty sets for keys in a dictionary and add values to the set naughtysensei 1 3,556 Nov-03-2020, 08:32 AM
Last Post: DeaD_EyE
  Making a dictionary from a file instyabam 0 1,964 Oct-27-2020, 11:59 AM
Last Post: instyabam
  Convert all actions through functions, fill the dictionary from a file Astone 3 3,401 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  how to put text file in a dictionary infected400 2 3,834 Jan-06-2019, 04:43 PM
Last Post: micseydel
  Dictionary to .txt or .csv file stanthaman42 9 6,431 Aug-08-2018, 03:37 PM
Last Post: Vysero

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020