CSV Dictionary

JC chalao.adda at gmail.com
Mon Dec 29 10:35:48 EST 2014


Hello,

I have csv file like:

id,name,userid,password
1,ABC,def at ghi,1234
2,DEF,ghi at jkl,asdf
3,GHI,jkl at mno,zxcv
.
.
.

I need to get that csv file into a dictionary. Here is my code:

import csv

with open('x.csv','rb') as f:
    rdr = csv.DictReader(f,delimiter=',')
    flds = rdr.fieldnames
    rows = 0
    d = {}
    for row in rdr:
        rows += 1
        i = 0
        for i in range(len(flds)):
            d[flds[i]] = row[flds[i]]

It shows only the last record, like:
d = {'Password': 'zxcv', 'UserId': 'jkl at mno', 'id': '3', 'Name': 'GHI'}

How could I get the all the records?

Thanks.



More information about the Python-list mailing list