How to read a csv file into a multidimensional list in Python

Question:

I’m trying to read a csv file into a multidimensional list with 52 rows and 7 columns. Currently it’s only displaying me the last line as 52 rows of the csv file.

I am pretty sure there is something wrong in my readfile function but I couldn’t figure it out where I’m making the mistake.

Here is my code:

rows = 52
cols = 7

def readFile():

  matrix = []

  file = open("rainfall.csv","r")
  for line in file:
    data = line.split(",")

  for row in range(rows):
    matrix.append([])
    for col in range(cols):
        matrix[row].append(data[col])
  
  return matrix
      
def display(matrix):
  for counter in matrix:
    for values in counter:
      print(values, end=" ")
    print()

matrix = readFile()
display(matrix)

Here is the output:

8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0 
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0
8 25 1 6 21 23 0

I have the following csv file:

0,0,30,2,21,13,23
29,3,29,30,7,8,25
26,5,26,13,4,13,4
22,30,13,15,15,0,2
3,12,11,10,17,0,15
8,13,11,24,30,24,27
22,18,2,29,11,13,18
15,1,29,23,18,7,0
23,27,3,7,13,14,28
6,25,24,14,20,23,5
24,29,26,22,0,9,18
22,27,22,20,24,29,21
23,13,14,4,13,1,21
25,21,21,6,28,17,19
4,6,11,10,21,1,5
11,7,22,11,10,24,15
25,11,23,3,23,8,3
22,23,0,29,15,12,5
21,11,18,22,1,4,3
11,10,3,1,30,14,22
2,16,10,2,12,9,9
2,29,17,16,13,18,7
22,15,27,19,6,26,11
21,7,18,4,14,14,2
6,30,12,4,26,22,11
21,16,14,11,28,20,3
19,10,22,18,30,9,27
8,15,17,4,11,16,6
19,17,16,6,18,18,6
2,15,3,25,27,16,11
15,5,26,24,24,30,5
15,11,16,22,14,23,28
25,6,7,20,26,18,16
5,5,21,22,24,16,5
6,27,11,8,24,1,16
28,4,1,4,3,19,24
19,3,27,14,12,24,0
6,3,26,15,15,22,26
18,5,0,14,15,7,26
10,5,12,22,8,7,11
11,1,18,29,6,9,26
3,23,2,21,29,15,25
5,7,1,6,15,18,24
28,11,0,6,28,11,26
4,28,9,24,11,13,2
6,2,14,18,20,21,1
20,29,22,21,11,14,20
28,23,14,17,25,3,18
6,27,6,20,19,5,24
25,3,27,22,7,12,21
12,22,8,7,0,11,8
8,25,1,6,21,23,0
Asked By: Mateo

||

Answers:

Change

 for line in file:
    data = line.split(",")

to:

 data=[]
 for line in file:
    data.append(line.split(","))
Answered By: Khaled DELLAL

Use the built-in csv module:

import csv
from pprint import pprint

with open('input.csv', newline='') as f:
    reader = csv.reader(f)
    data = [[int(x) for x in line] for line in reader]

pprint(data)

Output:

[[0, 0, 30, 2, 21, 13, 23],
 [29, 3, 29, 30, 7, 8, 25],
 [26, 5, 26, 13, 4, 13, 4],
 [22, 30, 13, 15, 15, 0, 2],
 [3, 12, 11, 10, 17, 0, 15],
 [8, 13, 11, 24, 30, 24, 27],
 [22, 18, 2, 29, 11, 13, 18],
 [15, 1, 29, 23, 18, 7, 0],
 [23, 27, 3, 7, 13, 14, 28],
 [6, 25, 24, 14, 20, 23, 5],
 [24, 29, 26, 22, 0, 9, 18],
 [22, 27, 22, 20, 24, 29, 21],
 [23, 13, 14, 4, 13, 1, 21],
 [25, 21, 21, 6, 28, 17, 19],
 [4, 6, 11, 10, 21, 1, 5],
 [11, 7, 22, 11, 10, 24, 15],
 [25, 11, 23, 3, 23, 8, 3],
 [22, 23, 0, 29, 15, 12, 5],
 [21, 11, 18, 22, 1, 4, 3],
 [11, 10, 3, 1, 30, 14, 22],
 [2, 16, 10, 2, 12, 9, 9],
 [2, 29, 17, 16, 13, 18, 7],
 [22, 15, 27, 19, 6, 26, 11],
 [21, 7, 18, 4, 14, 14, 2],
 [6, 30, 12, 4, 26, 22, 11],
 [21, 16, 14, 11, 28, 20, 3],
 [19, 10, 22, 18, 30, 9, 27],
 [8, 15, 17, 4, 11, 16, 6],
 [19, 17, 16, 6, 18, 18, 6],
 [2, 15, 3, 25, 27, 16, 11],
 [15, 5, 26, 24, 24, 30, 5],
 [15, 11, 16, 22, 14, 23, 28],
 [25, 6, 7, 20, 26, 18, 16],
 [5, 5, 21, 22, 24, 16, 5],
 [6, 27, 11, 8, 24, 1, 16],
 [28, 4, 1, 4, 3, 19, 24],
 [19, 3, 27, 14, 12, 24, 0],
 [6, 3, 26, 15, 15, 22, 26],
 [18, 5, 0, 14, 15, 7, 26],
 [10, 5, 12, 22, 8, 7, 11],
 [11, 1, 18, 29, 6, 9, 26],
 [3, 23, 2, 21, 29, 15, 25],
 [5, 7, 1, 6, 15, 18, 24],
 [28, 11, 0, 6, 28, 11, 26],
 [4, 28, 9, 24, 11, 13, 2],
 [6, 2, 14, 18, 20, 21, 1],
 [20, 29, 22, 21, 11, 14, 20],
 [28, 23, 14, 17, 25, 3, 18],
 [6, 27, 6, 20, 19, 5, 24],
 [25, 3, 27, 22, 7, 12, 21],
 [12, 22, 8, 7, 0, 11, 8],
 [8, 25, 1, 6, 21, 23, 0]]
Answered By: Mark Tolonen

Please use the stdlib’s csv module for this. It is designed for this exact purpose. Also, use the with statement for opening files. It will handle closing the file for you, even in the case that an exception is raised.

import csv

def readFile():
    with open("rainfall.csv","r") as f:
        matrix = list(csv.reader(f))
    return matrix
Answered By: Michael Ruth

Just on 4 lines:

#Read data file
with open('rainfall.csv', 'r') as f:
  lst = [[int(num) for num in line.split(',')] for line in f]

#Display result
for i in lst:
  print(*i)
Answered By: AziMez
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.