How about using Numpy? sp.csv might look like this:
0,0,0,4.1
1,1,2,5.2
0,1,1,3.2
Then, using Numpy, reading from file become a one-liner:
import numpy as np
sp = np.loadtxt('sp.csv', delimiter=',')
This yields a 2D record array:
array([[ 0. , 0. , 0. , 4.1],
[ 1. , 1. , 2. , 5.2],
[ 0. , 1. , 1. , 3.2]])
Converting this sparse matrix to a full ndarray works like this, assuming 0-based indexing. I'm not happy with the idx= line (there must be a more direct way), but it works:
max_indices = sp.max(0)[:-1]
fl = np.zeros(max_indices + 1)
for row in sp:
idx = tuple(row[:-1].astype(int))
fl[idx] = row[-1]
Resulting in the following ndarray fl:
array([[[ 4.1, 0. , 0. ],
[ 0. , 3.2, 0. ]],
[[ 0. , 0. , 0. ],
[ 0. , 0. , 5.2]]])