[SciPy-user] scipy.sparse: read file

Nathan Bell wnbell@gmail....
Wed Nov 12 11:32:58 CST 2008


On Wed, Nov 12, 2008 at 8:11 AM, Uwe Schmitt
<rocksportrocker@googlemail.com> wrote:
> I found a solution:
>
>    fp = file(...)
>
>    fp.next()
>    num_data = int(fp.next())
>
>    data = zeros((num_data,), dtype=....)
>    ij   = zeros((2, num_data), dtype=int)
>
>    for k, row in enumerate(fp):
>        i, j, val = row.split()
>
>        ij[0, k] =  int(i)
>        ij[1, k] =  int(j)
>        data[k]  =  float(val)
>
>    mat = csc_matrix((data, ij))
>

Yep, that will work.  Note that in SciPy 0.7 the .save() method is
deprecated.  Better alternatives include mmwrite() and savemat() in
scipy.io:
http://projects.scipy.org/scipy/scipy/browser/trunk/scipy/io/mmio.py#L55
http://projects.scipy.org/scipy/scipy/browser/trunk/scipy/io/matlab/mio.py#L119

These functions also have corresponding read methods: mmread() and loadmat().
-- 
Nathan Bell wnbell@gmail.com
http://graphics.cs.uiuc.edu/~wnbell/


More information about the SciPy-user mailing list