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
Post a Comment