Python import csv to list

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

    Update for Python3:

    import csv
    from pprint import pprint
    
    with open('text.csv', newline='') as file:
        reader = csv.reader(file)
        res = list(map(tuple, reader))
    
    pprint(res)
    

    Output:

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

    If csvfile is a file object, it should be opened with newline=''.
    csv module

提交回复
热议问题