5

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?

Community
  • 1
  • 1
Torger597
  • 175
  • 5

2 Answers2

5

@Paulo Scardine has a great answer if you want to create an exact dataset from the given csv. If you want to combine the values based on the key one could use this:

changes = {}
with open('test.csv', 'r') as f:
    for row in f:
        for e in row.rstrip('\n').split(", ") : #split lines by column
            print (e) #just to show what is being generated here
            (k,v) = e.split(":") #split further into key, value pairs
            changes.setdefault(k, []).append(v)
            #creates empty list if new key, adds value to list

print (changes)

Data will look like:

{'name3': ['name5'], 'name2': ['name4', 'name6', 'name5'], 'name1': ['name', 'name4'], 'name': ['name2', 'name3']}

This could be further simplified but I think this gives the good example that someone learning can follow.

Edit: added setdefault method following @Paulo Scardine comment

LinkBerest
  • 1,281
  • 3
  • 22
  • 30
  • 1
    This idiom is so common in Python that there is some syntax sugar for it: see `dict.setdefault` and `collections.defaultdict`. – Paulo Scardine Mar 19 '15 at 03:58
  • Neat, I've been moving from R to Python for my AI class and those methods might save me some headaches along the road – LinkBerest Mar 19 '15 at 04:05
3

Try this:

data = []
with open('names.dat') as database:
    for line in database:
        if line.strip():  # skip blank lines
            data.append(
                dict(i.split(":") for i in line.rstrip('\n').split(","))
            )

If your file is:

name:name2,name:name3,name2:name4,name3:name5
name:name2,name:name3,name2:name4,name3:name5
name:name2,name:name3,name2:name4,name3:name5
name:name2,name:name3,name2:name4,name3:name5

data will be:

[{'name': 'name3', 'name2': 'name4', 'name3': 'name5'},
 {'name': 'name3', 'name2': 'name4', 'name3': 'name5'},
 {'name': 'name3', 'name2': 'name4', 'name3': 'name5'},
 {'name': 'name3', 'name2': 'name4', 'name3': 'name5'}]

Perhaps you want a dict of list instead of a list of dict:

data = {}
with open('names.dat') as database:
    for line in database:
        if line.strip():  # skip blank lines
            for k, v in (i.split(":") for i in line.rstrip('\n').split(",")):
                data.setdefault(k, []).append(v)

Resulting:

{'name': [ 'name2', 'name3', 'name2', 'name3', 'name2', 'name3', 'name2', 'name3'],
 'name2': ['name4', 'name4', 'name4', 'name4'],
 'name3': ['name5', 'name5', 'name5', 'name5']}
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153