I have a homework problem in Python.
I am using Python version 3.4.0 on Linux.
The design document states that I am to read a CSV file using built in functions, specified as names.dat, that is in the format:
name:name2, name:name3, name2:name4, name3:name5\n (etc)
I am then to add these keyword pairs to a dictionary, which is the part I'm stuck on.
The code I have thus far is this:
dictionary = dict()
database = open('names.dat', 'r')
data = database.read()
data = data.rstrip('\n')
data = data.split(',')
for item in range(len(data)):
dictionary.update(data[item-1])
My thinking being that if I have a list element in the format "name:name2", and I call the dictionary update function with that element as an argument, it will properly map to a keyword pair in the dictionary.
However, this is not the case, as I get this error when I run this script:
File "MyName.py", line 7, in <module>
dictionary.update(data[item-1])
ValueError: dictionary update sequence element #0 has length 1; 2 is required
This and This seem similar, but I feel that this is enough of a different question to warrant a separate response.
What am I doing wrong here, and how can I fix it?
Is there a simpler way to do this?