How can I use the same keys, but have different values in a dictionary?
I have a table of Jeopardy questions:
Show Number Air Date Round Category Value Question Answer
4680 12/31/2004 Jeopardy! HISTORY $200 Question 1 Copernicus
4680 12/31/2004 Jeopardy! ESPN $200 Question 2 Jim Thorpe
4680 12/31/2004 Jeopardy! EVERYBODY TALKS $200 Question 3 Arizona
4680 12/31/2004 Jeopardy! THE COMPANY LINE $200 Question 4 McDonald's
4680 12/31/2004 Jeopardy! EPITAPHS $200 Question 5 John Adams
(Note: this is stored in a csv file. I just tried to show its layout above. It's available here, fyi)
And basically, I'm trying to get a list/dictionary/return that has a header matched with a question, something like a variable that holds:
['Show Number':4680, 'Air Date': '12/31/2004', ...'Answer':'Copernicus']
['Show Number':4680, 'Air Date': '12/31/2004', ...'Answer':'Jim Thorpe']
['Show Number':4680, 'Air Date': '12/31/2004', ...'Answer':'Arizona']
So later on, I can parse through that dictionary(?) and do things like get the unique values based on Category, Value, etc. Would it be ..a list of a dictionary??
I tried making it a dictionary - and it doesn't work. It only returns the last row's data. I understand why, because each time the row changes, it just starts back and updates the same keys with new info.
import csv
file_name = 'JEOPARDY_CSV.csv'
def get_data(csv_file):
data = []
with open(csv_file, 'r', encoding="utf8") as read:
reader = csv.reader(read)
all_data = list(reader)
data = all_data[1:]
headers = all_data[0]
return data, headers
def create_dict(data, headers):
i = 0
data_dict = {}
for row in data:
for col in row:
data_dict[headers[i]] = col
i+=1
i = 0
print(data_dict)
def main():
file_data, headers = get_data(file_name)
data_dictionary = create_dict(file_data[0:5], headers)
if __name__ == "__main__":
main()
Again, the idea is to later on, have a function I can run to do things based on column header, like "return all questions where show number is 4680", or "for all categories, return the unique ones".