Python import csv to list

后端 未结 13 1094
后悔当初
后悔当初 2020-11-22 06:15

I have a CSV file with about 2000 records.

Each record has a string, and a category to it:

This is the firs         


        
13条回答
  •  抹茶落季
    2020-11-22 06:43

    Here is the easiest way in Python 3.x to import a CSV to a multidimensional array, and its only 4 lines of code without importing anything!

    #pull a CSV into a multidimensional array in 4 lines!
    
    L=[]                            #Create an empty list for the main array
    for line in open('log.txt'):    #Open the file and read all the lines
        x=line.rstrip()             #Strip the \n from each line
        L.append(x.split(','))      #Split each line into a list and add it to the
                                    #Multidimensional array
    print(L)
    

提交回复
热议问题