How would I adjust my Python code for a nested .JSON tree (Python dictionary) to include multiple keys?

Raf A

In my current code, it seems to only take into account one value for my Subject key when there should be more (you can only see Economics in my JSON tree and not Maths). I've tried for hours and I can't get it to work.

Here is my sample dataset - I have many more subjects in my full data set:

ID,Name,Date,Subject,Start,Finish
0,Ladybridge High School,01/11/2019,Maths,05:28,06:45
0,Ladybridge High School,02/11/2019,Maths,05:30,06:45
0,Ladybridge High School,01/11/2019,Economics,11:58,12:40
0,Ladybridge High School,02/11/2019,Economics,11:58,12:40
1,Loreto Sixth Form,01/11/2019,Maths,05:28,06:45
1,Loreto Sixth Form,02/11/2019,Maths,05:30,06:45
1,Loreto Sixth Form,01/11/2019,Economics,11:58,12:40
1,Loreto Sixth Form,02/11/2019,Economics,11:58,12:40

Here is my Python code:

timetable = {"Timetable": []}
with open("C:/Users/kspv914/Downloads/Personal/Project Dawn/Timetable Sample.csv") as f:
    csv_data = [{k: v for k, v in row.items()} for row in csv.DictReader(f, skipinitialspace=True)]

    name_array = []
    for name in [row["Name"] for row in csv_data]:
        name_array.append(name)
    name_set = set(name_array)

    for name in name_set:
        timetable["Timetable"].append({"Name": name, "Date": {}})

    for row in csv_data:
        for entry in timetable["Timetable"]:
            if entry["Name"] == row["Name"]:
                entry["Date"][row["Date"]] = {}
                entry["Date"][row["Date"]][row["Subject"]] = {
                    "Start": row["Start"],
                    "Finish": row["Finish"]
                }

Here is my JSON tree: JSON Tree

Jay Patel

You're making date dict empty and then adding a subject.

Do something like this:

timetable = {"Timetable": []}
with open("a.csv") as f:
    csv_data = [{k: v for k, v in row.items()} for row in csv.DictReader(f, skipinitialspace=True)]

    name_array = []
    for name in [row["Name"] for row in csv_data]:
        name_array.append(name)
    name_set = set(name_array)

    for name in name_set:
        timetable["Timetable"].append({"Name": name, "Date": {}})

    for row in csv_data:
        for entry in timetable["Timetable"]:
            if entry["Name"] == row["Name"]:
                if row["Date"] not in entry["Date"]:
                    entry["Date"][row["Date"]] = {}
                entry["Date"][row["Date"]][row["Subject"]] = {
                    "Start": row["Start"],
                    "Finish": row["Finish"]
                }

I've just added if condition before assigning {} to entry["Date"][row["Date"]]

It will give output like as shown in the below image: enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to modify a specific set of values in a nested dictionary with multiple keys in Python?

How do I remove certain keys from a nested dictionary in Python?

How can I search for specific keys in this nested dictionary in Python?

How can I change keys in a nested dictionary in Python

How do I use numbers in a list as keys for my dictionary in Python?

How can I add multiple keys to a python Dictionary?

How Can I Find Multiple Keywords in Dictionary Keys in Python

How do I configure multiple dictionary keys in a yaml file in Python?

how to access values of nested dictionary keys in python?

How To Capture Dynamic Keys In Nested Python Dictionary

Python: How to pass multiple keys at a time to dictionary

How to add multiple values to dictionary keys in Python

How to handle multiple keys for a dictionary in python?

Python generator for nested dictionary keys

python: dynamic keys in nested dictionary

How can I adjust my Python subprocess code so that the python scripts run in parallel?

How to display multiple values in a nested dictionary in python?

How do I format JSON's with multiple of the same keys with python?

How to split dictionary keys into multiple separate keys in python?

How can i create a string of a nested dictionary(no loops) as a series of branches of its corresponding tree in python

How to get nested dictionary from JSON in Python

how do i make a nested dictionary in python?

Python dictionary list with multiple keys

keys with multiple values in a dictionary (Python)

Adding multiple keys to the dictionary (python)

How to print all the keys and values together from a nested dictionary in Python?

How to convert the keys of Python dictionary to string for all nested dictionaries too

Python how to sort a list of nested dictionaries by dictionary keys?

How can I correct my code to produce a nested dictionary?