Converting csv file to dictionary

后端 未结 2 717
刺人心
刺人心 2020-12-22 13:49

I asked this question yesterday but am still stuck on it. I\'ve written a function that currently reads a file correctly but there are a couple of problems.

The main

2条回答
  •  半阙折子戏
    2020-12-22 14:09

    This should simplify your code a bit, however I have left dealing with the header row up to you.

    from collections import defaultdict
    import csv
    
    artists = defaultdict(list)
    
    with open('artists.csv', 'r') as csvfile:
        reader = csv.reader(csvfile,delimiter=',')
        for row in reader:
            artists[row[0]].append(row[1:-1])
    

提交回复
热议问题