1

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

2
  • Can you please specify/elaborate on your exact problem? Commented Nov 21, 2019 at 12:44
  • In my JSON tree, you can only see the subject Economics...where has Maths gone? Commented Nov 21, 2019 at 12:47

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Much appreciated. Any idea how I would add additional data (no nested data) that is on the same level as "Name" (no nested data) and "Date" (contains nested data)?
0

You are overwriting your dict entries with entry["Date"][row["Date"]][row["Subject"]] =. The first time "math" is met, the entry is created. The second time it is overwritten.

Your expected result should be a list, not a dict. Every entry should be appended to the list with timetable_list.append().

Here is a simple code that converts the whole csv file into Json without loosing data:

import csv
import json

data = []

with open("ex1.csv") as f:
    reader = csv.DictReader(f)
    for row in reader:
        data.append(row)

print(json.dumps({"Timetable": data}, indent=4))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.