We do get questions about "how to read matrix from csv" quite regularly. Of
course its just a few lines of code, but I think it would be nice to have a
matrix_from_file('fname.csv') function that imports csv and perhaps others
(gnumeric/ooffice/excel). Any volunteers? ;-)
On Friday, February 24, 2012 10:03:05 AM UTC-8, Harald Schilly wrote:
>
>
>
> On Friday, February 24, 2012 11:13:53 AM UTC+1, Chappman wrote:
>>
>> and then using a function which opens up the CSV file and utilizes the
>> entires in the matrix P, from the CSV file.
>> Is there a method for this?
>
>
> uhm, i'm not sure if you ask about reading or writing. also, your "d" in
> def is uppercase.
>
> reading from this file matrix.csv:
> 1,2,3
> 2,2,-1.1
> 0,0,1
>
>
> works like this:
> sage: import csv
> sage: data = list(csv.reader(file("matrix.csv")))
> sage: m = matrix([[ float(_) for _ in line] for line in data])
> sage: m
> [ 1.0 2.0 3.0]
> [ 2.0 2.0 -1.1]
> [ 0.0 0.0 1.0]
>
> the other way around works like this (i print this, writing to the file is
> trivial)
>
> for line in m.rows():
> print ','.join((str(_) for _ in line))
>
> gives
>
> 1.0,2.0,3.0
> 2.0,2.0,-1.1
> 0.0,0.0,1.0
>
>
> h
>
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org