Converting csv file to dictionary

后端 未结 2 714
刺人心
刺人心 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 13:51

    You just want this:

    def read_file(filename):
        with open(filename, "r") as thefile:
            mydict = convertLines(thefile.readlines()))
            return mydict
    

    your current function is infinitely calling itself... Then fix your convertLines function if it needs to.

    0 讨论(0)
  • 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])
    
    0 讨论(0)
提交回复
热议问题