Python import csv to list

后端 未结 13 1306
后悔当初
后悔当初 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:51

    Unfortunately I find none of the existing answers particularly satisfying.

    Here is a straightforward and complete Python 3 solution, using the csv module.

    import csv
    
    with open('../resources/temp_in.csv', newline='') as f:
        reader = csv.reader(f, skipinitialspace=True)
        rows = list(reader)
    
    print(rows)
    

    Notice the skipinitialspace=True argument. This is necessary since, unfortunately, OP's CSV contains whitespace after each comma.

    Output:

    [['This is the first line', 'Line1'], ['This is the second line', 'Line2'], ['This is the third line', 'Line3']]
    

提交回复
热议问题