python - Can't build two dictionaries -


i want build 2 similar dictionaries same csv file (one mapping row[0] row[5], 1 row[0] row[6]. reason, can build one. whichever of these 2 lines run first, dictionary exists , want. other empty. no idea what's going wrong.

mydict = {row[0]:row[5] row in csv.reader(g)} mydict1 = {row[0]:row[6] row in csv.reader(g)} 

i using python 3, , tried changing dictionary name , few other things.

you cannot iterate on file twice without rewinding start. doing not efficient either; better not use dictionary comprehensions here:

mydict1, mydict2 = {}, {} row in csv.reader(g):     mydict1[row[0]] = row[5]     mydict2[row[0]] = row[6] 

if insist, can use file.seek(0) put file pointer start; don't need re-create reader here:

reader = csv.reader(g) mydict1 = {row[0]:row[5] row in reader} g.seek(0) mydict2 = {row[0]:row[6] row in reader} 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -