python 2.7 - Dictionary Key Error -
i trying construct dictionary values csv file.say 10 columns there , want set first column key , remaining values. if setting loop dictionary has have 1 value. kindly suggest me way.
import csv import numpy aname = {} #loading file in numpy result=numpy.array(list(csv.reader(open('somefile',"rb"),delimiter=','))).astype('string') #devolop dict\ r = {aname[rows[0]]: rows[1:] rows in result} print r[0]
error follows.
r = {aname[rows[0]]: rows[1:] rows in result}
keyerror: '2a9ac84c-3315-5576-4dfd-8bc34072360d|11937055'
i'm not entirely sure mean here, help:
>>> result = [[1, 'a', 'b'], [2, 'c', 'd']] >>> dict([(row[0], row[1:]) row in result]) {1: ['a', 'b'], 2: ['c', 'd']}
Comments
Post a Comment