I'm currently import some data from a csv and want to add it to separate dictionaries dependant which row I'm iterating through. I've added my dictionaries to a list which I hoped would allow me to index the dictionary from the list dependant which row I'm currently iterating through.
I have however come across a stumbling block when I make the row equal to the list index I believe it makes the instance of the dictionary in the list equal to the row not the dictionary itself (this is what I think is happening not 100% sure, this is my guess from my print statements at the bottom of my code). I would like to make the dictionary itself to equal that row not the version of it in the list, my code is below any help will be much apricated and thanks in advance.
import csv
one_information = {}
two_information = {}
three_information = {}
four_information = {}
five_information = {}
six_information = {}
seven_information = {}
lst = [one_information, two_information, three_information, four_information, five_information, six_information, seven_information]
with open('testing.csv', mode = 'r',encoding='utf-8-sig') as csv_file:
csv_reader = csv.DictReader(csv_file)
for index, row in enumerate(csv_reader):
if row['Room Name'] == 'test':
lst[index] = row
else:
pass
print(one_information)
print (lst[0])
CodePudding user response:
You are right, what you did is assigning list element under specified index from dictionary to row
. Whay you probably mean is to update
your dictionary:
lst[index].update(row)
# or for python 3.9
# lst[index] |= row