0
votes

I want to create a simple config CSV file for my python script that will be loaded into a dictionary. Every line should represent a dictionary key with a list of values.

The structure will look like this:

key, value1, value2, value3, value4
key, value1, value2, value3, value4
key, value1, value2, value3, value4

A print (list) looks like this:

['key,value1,value2,value3,value4','key,value1,value2,value3,value4','key,value1,value2,value3,value4']

My function for reading input file gives me back a list with element per line. Here I try to build the dictionary with the split string function in a loop. But I cannot get the syntax right. I have found some similar examples, but they don't seem to work in python 3.

result = dict(line.split(",")[0],[line.split(",")[1:] for line in list)

1
Can you please provide some sample input?Kurt Kline

1 Answers

3
votes

Use a dict comprehension.
Oh, and try to avoid calling variables the same name as built-ins (so I've called it lst instead):

result = {line.split(',')[0]: line.split(',')[1:] for line in lst}