Read two column CSV as dict with 1st column as key

后端 未结 2 1677
春和景丽
春和景丽 2020-12-21 16:44

I have a CSV with two columns, column one is the team dedicated to a particular building in our project.

The second column is the actual building number.

Wha

2条回答
  •  独厮守ぢ
    2020-12-21 16:56

    This works:

    import csv
    
    result={}
    with open('/tmp/test.csv','r') as f:
        red=csv.DictReader(f)
        for d in red:
            result.setdefault(d['team'],[]).append(d['bldg'])
    
    #results={'1': ['1450'], '3': ['204', '250', '1437'], '2': ['1440']}
    

提交回复
热议问题