Manolete
Manolete

Reputation: 3517

Storing a variable number of values per item

I want to read and store in a defaultdict(list) a csv file:

Pos   ID    Name
1   0001L01 50293
2   0002L01 128864
3   0003L01 172937
4   0004L01 12878
5   0005L01 demo
6   0004L01 12878
7   0004L01 12878
8   0005L01 demo

I would like the ID to be my keys and as values Pos and Name. However the number of Pos varies. For instance ID 0005L01 contains Pos 8 and 5 whereas 0001L01 contains Pos 1. Is there a way of doing that? So far I got:

reader = csv.reader(open("sheet.csv", "rb"))
for row in reader:
       if any(row):
          dlist.append(row)

for k, g in groupby(zip(mylist, itertools.count()), key=lambda x: x[0][1]):
            map(lambda x: d[k].append((x[0][0], x[1], x[0][2])), g)

Upvotes: 2

Views: 56

Answers (1)

Kasravnd
Kasravnd

Reputation: 107347

You can use dict.setdefault method to create the expected dictionary:

import csv
d={}
with open('my_file.csv', 'rb') as csvfile:
     spamreader = csv.reader(csvfile, delimiter=' ')
     for row in spamreader:
     try :
         Pos,ID,Name=row
         d.setdefault(ID,[]).append([Pos,Name])
     except ValueError :
          continue

result:

{'0001L01': [['1', '50293']],
 '0003L01': [['3', '172937']], 
 '0002L01': [['2', '128864']], 
 '0005L01': [['5', 'demo'], ['8', 'demo']], 
 '0004L01': [['4', '12878'], ['6', '12878'], ['7', '12878']]}

As @tobias_k says, if you have not pos columns in your file you can use enumerate to create it manually :

import csv
d={}
with open('my_file.csv', 'rb') as csvfile:
     spamreader = csv.reader(csvfile, delimiter=' ')
     for Pos,row in enumerate(spamreader,1):
     try :
         ID,Name=row
         d.setdefault(ID,[]).append([Pos,Name])
     except ValueError :
          continue

Upvotes: 2

Related Questions