Search code examples
pythonpandaslistcsvnested-lists

How to transform a csv file into a multi-dimensional list using Python?


I started out with a 4d list, something like

tokens = [[[["a"], ["b"], ["c"]], [["d"]]], [[["e"], ["f"], ["g"]],[["h"], ["i"], ["j"], ["k"], ["l"]]]]

So I converted this to a csv file using the code

import csv
def export_to_csv(tokens):
    csv_list = [["A", "B", "C", word]]
    for h_index, h in enumerate(tokens):
        for i_index, i in enumerate(h):
            for j_index, j in enumerate(i):
                csv_list.append([h_index, i_index, j_index, j])
    
    with open('TEST.csv', 'w') as f:
      
        # using csv.writer method from CSV package
        write = csv.writer(f)

        write.writerows(csv_list)

But now I want to do the reverse process, want to convert a csv file obtained in this format, back to the list format mentioned above.


Solution

  • Assuming you wanted your csv file to look something like this (there were a couple typos in the posted code):

    A,B,C,word                                                                          
    0,0,0,a                                                                             
    0,0,1,b                                                                             
    0,0,2,c
    ...
    

    here's one solution:

    import csv                                                                          
                                                                                        
    def import_from_csv(filename):                                                      
        retval = []                                                                     
        with open(filename) as fh:                                                      
            reader = csv.reader(fh)                                                     
            # discard header row                                                        
            next(reader)   
            # process data rows                                                             
            for (x,y,z,word) in reader:                                                 
                x = int(x)                                                              
                y = int(y)                                                              
                z = int(z)                                                              
                retval.extend([[[]]] * (x + 1 - len(retval)))                           
                retval[x].extend([[]] * (y + 1 - len(retval[x])))                       
                retval[x][y].extend([0] * (z + 1 - len(retval[x][y])))                  
                                                                                        
                retval[x][y][z] = [word]                                                
                                                                                        
        return retval