I am trying to read a csv file into a dictionary where for each row, the first column of the csv file will be a dictionary key and the remaining columns will be the values in a list (ignoring the header row).
I have got a solution that works but I have hardcoded the columns and what I want is something that works on any number of columns (because I have multiple csv files with different numbers of columns that I want to use it on).
My data in one of the csv files is:
name,test1,test2,test3
dave,66,74,62
rob,59,65,60
nic,71,68,73
This is the code that I have used
import sys
import csv
# Create a dict (where the values are a list) to store the data in memory
database = {}
# Open the csv file and read the contents into memory
filename = sys.argv[1]
with open(filename, "r") as file:
reader = csv.reader(file)
# Ignore the header
next(reader)
for row in reader:
# Read the first column of the csv (name) as the key, then read the remaining columns as a list for the values
database[row[0]] = [int(row[1]), int(row[2]), int(row[3])]
print(database)
The output for the print(database) is
{'dave': [66, 74, 62], 'rob': [59, 65, 60], 'nic': [71, 68, 73]}
The problem I have is that when I need to read in another csv file with more columns, this code will only work for 4 columns (unless I extend it) for row[4], row[5] etc...
Is there a way I can rewrite the code to just read in row[1] all the way through to row[n] in the csv file? Where n is the number of the last column in the csv file.
Any help is greatly appreciated. Just started learning Python, I have been searching but I wasn't able to find anything to help. Thanks in advance.
question from:
https://stackoverflow.com/questions/65876960/how-to-read-a-csv-into-a-dict-where-the-values-are-a-list-then-use-this-for-di