I am reading information from a CSV file and I am using a nested dictionary to map out the repetitive information in the file. How do I go about creating a nested dictionary for this file for all rows of the file? An example of the data (not actual data but basically same concept)
State ,City/Region ,Questions ,Answers
NY,Manhattan ,East/West Coast? ,East
NY,Manhattan ,been there? ,yes
NY,Brooklyn ,East/West Coast? ,East
NY,Brooklyn ,been there? ,yes
NY,Brooklyn ,Been to coney island? ,yes
NY,Queens ,East/West Coast? ,East
NY,Queens ,been there? ,yes
NY ,Staten Island ,is island? ,yes
MA,Boston ,East/West Coast? ,East
MA,Boston ,like it there? ,yes
MA,Pioneer Valley ,East/West Coast? ,East
MA,Pioneer Valley ,city? ,no
MA,Pioneer Valley ,college town? ,yes
CA,Bay Area ,warm? ,yes
CA ,Bay Area ,East/West Coast? ,West
CA ,SoCal ,north or south? ,south
CA ,SoCal ,warm ,yes
So essentially, the master dictionary has 3 keys: NY, MA, CA, each of them has a dictionary with City/Region as key, and each City/Region has the questions and answers.
So it would be a very nested dictionary but I can't figure out the syntax for this to do it for every row in the file.
I've tried opening the file, used a for loop to read through the lines and split the lines by ",". Something like this:
for line in my_file:
line=line.split(",")
MasterDict[line[0]] = {line[1] : {} }
MasterDict[line[0]][line[1]] = {line[2] : line[3]}