0

I need help on converting a csv file to a dictionary. My professor isn't the best teacher and since this semester is kind of rush, he barely taught us anything and gives us an assignment that none of us can figure out. He essentially told us to figure how to code on our own. It is an intro level class so it's the first I learn anything about coding and I am currently struggling in it because the professor never seems to teach or reply back to my email. I know I am asking for a lot, but I really appreciate if someone can at least explain to me how to type this code or give a sample of code with a reason why you are using that code, so I can understand it better.

Here is a sample of my code and the instructions. I give it my best shot, but he didn't explain anything so it was a shot in the dark.

Please mind my writing as English wasn't my first language.

Instructions 1.write a function call data_set. You will load and convert this dataset, call the voter_timeline(this is the dataset, from a text file into a dictionary. Specifically, there are two approaches you can use - the csv.reader or the csv.DictReader functions - will allow you to load CSV files. For this project, you will be loading the CSV into a dictionary. Each state's name will serve as the key to access a second dictionary containing vital information about the state. In this inner dictionary, you will have two keys required for each state - county and county_names.

-county will contain a boolean value, indicating whether the country has record state data

-county_names will contain a list of the recorded state names. These will be useful when determining if the user entered a valid county. Each entry should also include a key for the county listed. Note, these need to be converted from Strings into Integers.

Some states may not have counties, so create an if statement to make sure the user can still retrieve the information about the voters in the state, instead of county.

The excel below is a small sample voter_timeline and you need to convert to a dictionary with 50 states and 3141 counties. excel record

Here is my code

my code

  • this could be helpful https://stackoverflow.com/questions/6740918/creating-a-dictionary-from-a-csv-file – SebNik Oct 26 '20 at 13:39

1 Answers1

0

So first of all there are two simple solutions that should work:
You could try it with pandas or with the csv.reader. The simpler but less elegant way is with pandas and goes like this:

import pandas as pd
pd.read_csv('coors.csv', header=None, index_col=0, squeeze=True).to_dict()

The other way would be:

import csv
reader = csv.reader(open('filename.csv', 'r'))
d = {}
for k, v in reader:
   d[k] = v
SebNik
  • 880
  • 3
  • 10
  • 21
  • But there is a dictionary nested within a dictionary(The state is one dictionary and then the county and county_name are within it) what should I do here? I'm so sorry for asking so many questions. – Jennifer Li Oct 26 '20 at 15:31
  • okay instead of v you can create a new dictionary and and build this with the column heads and the v data that would work – SebNik Oct 26 '20 at 15:39
  • But the second needs to be nested inside the first dictionary. I think that is where I am having the most trouble with it. – Jennifer Li Oct 26 '20 at 16:09
  • yeah got it just build a new one – SebNik Oct 26 '20 at 16:23