Reading rows from a CSV file in Python

前端 未结 10 2383
慢半拍i
慢半拍i 2020-11-30 23:58

I have a CSV file, here is a sample of what it looks like:

Year:  Dec: Jan:
1      50   60
2      25   50
3      30   30
4      40   20
5      10   10
         


        
10条回答
  •  温柔的废话
    2020-12-01 00:41

    The Easiest way is this way :

    from csv import reader
    
    # open file in read mode
    with open('file.csv', 'r') as read_obj:
        # pass the file object to reader() to get the reader object
        csv_reader = reader(read_obj)
        # Iterate over each row in the csv using reader object
        for row in csv_reader:
            # row variable is a list that represents a row in csv
            print(row)
    
    output:
    ['Year:', 'Dec:', 'Jan:']
    ['1', '50', '60']
    ['2', '25', '50']
    ['3', '30', '30']
    ['4', '40', '20']
    ['5', '10', '10']
    

提交回复
热议问题