Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make dictionary from csv file columns

Tags:

python

i am new to the concept of dictionaries in python. I have a csv file with multiple columns and i want to create a dictionary such that keys are taken from 1st column and values from the second and a key:value pair is made for all rows of those two columns. The code is as follows:

    if __name__=="__main__":
reader = csv.reader(open("file.csv", "rb"))
for rows in reader:
        k = rows[0]
        v = rows[1]
        mydict = {k:v}
print (mydict)

problem: The output returned is only for "last" or "bottom most" row of the first two columns i.e. {'12654':'18790'}. i want the dictionary to contain all 100 rows of the first two columns in this format. How to do that? can i run some loop on the row numbers for the first two columns to do that...i dont know how.

like image 420
user899714 Avatar asked Dec 22 '22 09:12

user899714


2 Answers

if __name__=="__main__":
    mydict = {}
    reader = csv.reader(open("file.csv", "rb"))
    for rows in reader:
        k = rows[0]
        v = rows[1]
        mydict[k] = v
    print mydict

Here:

mydict = {k:v}

You were making new dictionary in every iteration, and the previous data has been lost.

Update:

You can make something like this:

mydict = {}
L = [(1, 2), (2, 4), (1, 3), (3, 2), (3, 4)]
for el in L:
    k, v = el
    if not k in mydict:
        mydict[k] = [v]
    else:
        mydict[k].append(v)

print mydict

>>> 
{1: [2, 3], 2: [4], 3: [2, 4]}

This way, each value of the same key will be stored

Your code will be:

if __name__=="__main__":
    mydict = {}
    reader = csv.reader(open("file.csv", "rb"))
    for i, rows in enumerate(reader):
        if i == 0: continue
        k = rows[0]
        v = rows[1]
        if not k in mydict:
            mydict[k] = [v]
        else:
            mydict[k].append(v)

    print mydict

Update2: You mean?

for k, v in mydict.items():
    print "%s: %s" % (k, v)

>>>
1: [2, 3]
2: [4]
3: [2, 4]

Update3:

This should work:

if __name__=="__main__":
        mydict = {}
        reader = csv.reader(open("file.csv", "rb"))
        for i, rows in enumerate(reader):
            if i == 0: continue
            k = rows[0]
            v = rows[1]
            if not k in mydict:
                mydict[k] = [v]
            else:
                mydict[k].append(v)

        print mydict
like image 166
develerx Avatar answered Dec 24 '22 02:12

develerx


You are creating a new dict and overwriting the old one each iteration. @develerx's answer fixes this problem. I just wanted to point an easier way, using dict comprehensions:

Assuming the csv file contains two columns.

if __name__=="__main__":
    reader = csv.reader(open("file.csv", "rb"))
    my_dict = {k: v for k, v in reader}
    print mydict

If you are using older version(older than 2.7 I think), you can't use dict comprehensions, just use the dict function then:

my_dict = dict((k, v) for k, v in reader)

Edit: And I just thought that; my_dict = dict(reader) could also work.

like image 22
utdemir Avatar answered Dec 24 '22 02:12

utdemir