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
#3
What is your input data?
Json-Data looks like your example.

For example, this is csv:
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:
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.

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


Messages In This Thread
RE: how can i create a dictionary of dictionaries from a file - by DeaD_EyE - Oct-26-2020, 02:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,316 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Newbie here. Create an array from file data? Rayj00 2 1,325 Jan-13-2023, 01:35 PM
Last Post: perfringo
  Using dictionary to find the most sent emails from a file siliusu 6 7,742 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Updating dictionary in another py file tommy_voet 1 5,041 Mar-28-2021, 07:25 PM
Last Post: buran
  Create a turtle drawing from .txt file Noob101 20 9,342 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 2,578 Nov-03-2020, 08:32 AM
Last Post: DeaD_EyE
  Making a dictionary from a file instyabam 0 1,561 Oct-27-2020, 11:59 AM
Last Post: instyabam
  Convert all actions through functions, fill the dictionary from a file Astone 3 2,530 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  how to put text file in a dictionary infected400 2 3,048 Jan-06-2019, 04:43 PM
Last Post: micseydel
  Dictionary to .txt or .csv file stanthaman42 9 4,831 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