ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 01 Jul 2013 10:02:29 +0200Compatible data file formatshttps://ask.sagemath.org/question/10300/compatible-data-file-formats/What are the compatible file formats if I want to load a data set into Sage? I know that I can load .*csv* files using Python's csv module or *open()* method.
Is there any **other file format** that I can use in Sage?
ThanksSun, 30 Jun 2013 16:20:45 +0200https://ask.sagemath.org/question/10300/compatible-data-file-formats/Answer by Nathann for <p>What are the compatible file formats if I want to load a data set into Sage? I know that I can load .<em>csv</em> files using Python's csv module or <em>open()</em> method. </p>
<p>Is there any <strong>other file format</strong> that I can use in Sage?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/10300/compatible-data-file-formats/?answer=15162#post-id-15162Well, you would have to be more accurate about WHAT you want to store if you want to know how Sage can store it. There is a very general-purpose format that Sage supports though, but it will mostly be useful if you want to send a "Sage object" to another Sage user.
sage: # Build an object containing whatever you want to store
sage: a = [1, Graph(), Matrix(2,2)]
sage: save(a,"/tmp/my_sage_object.sobj")
sage: load("/tmp/my_sage_object.sobj")
[
[0 0]
1, Graph on 0 vertices, [0 0]
]
sage: one,my_graph,my_matrix = load("/tmp/my_sage_object.sobj")
That's a pretty cool feature if you want to store an object.
But if what you had in mind is "how can I exchange data with other persons or load their files", then you have to be much more specific.
NathannSun, 30 Jun 2013 16:46:32 +0200https://ask.sagemath.org/question/10300/compatible-data-file-formats/?answer=15162#post-id-15162Comment by Tharindu Rusira for <p>Well, you would have to be more accurate about WHAT you want to store if you want to know how Sage can store it. There is a very general-purpose format that Sage supports though, but it will mostly be useful if you want to send a "Sage object" to another Sage user.</p>
<pre><code>sage: # Build an object containing whatever you want to store
sage: a = [1, Graph(), Matrix(2,2)]
sage: save(a,"/tmp/my_sage_object.sobj")
sage: load("/tmp/my_sage_object.sobj")
[
[0 0]
1, Graph on 0 vertices, [0 0]
]
sage: one,my_graph,my_matrix = load("/tmp/my_sage_object.sobj")
</code></pre>
<p>That's a pretty cool feature if you want to store an object.</p>
<p>But if what you had in mind is "how can I exchange data with other persons or load their files", then you have to be much more specific.</p>
<p>Nathann</p>
https://ask.sagemath.org/question/10300/compatible-data-file-formats/?comment=17415#post-id-17415Thanks Nathann. Actually what I wanted was to load some values in a file so we can do some plotting, statistics etc (like what we do by opening a .csv file). I wanted to know whether there are any other formats which I can directly load into Sage(e.g. ods, xls,...)
Sorry my question is vague. Mon, 01 Jul 2013 02:27:11 +0200https://ask.sagemath.org/question/10300/compatible-data-file-formats/?comment=17415#post-id-17415Answer by tmonteil for <p>What are the compatible file formats if I want to load a data set into Sage? I know that I can load .<em>csv</em> files using Python's csv module or <em>open()</em> method. </p>
<p>Is there any <strong>other file format</strong> that I can use in Sage?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/10300/compatible-data-file-formats/?answer=15165#post-id-15165Note that `open()` is a Python function that can open *any* kind of file. The main issue is then to handle your file so that the data can be used in Sage (i.e. transformed into Sage objects).
Since Sage is written in Python, there are many modules to handle many particular types of file (e.g. if you want to handle an xml file, you can imort and use the `xml` module, if you want to handle a gpg file, you can install, import and use the `gnupg` module, and so on). If your file is an ad-hoc (non-standard) text file, you can use various python tools to get interesting data from it (e.g. use the [string methods](http://docs.python.org/2/library/stdtypes.html#string-methods), consider also the [`re`](http://docs.python.org/2/library/re.html) module allows you to filter your file with regular expressions).
So this really depends on your situation. The data from which sotware do you want to deal with ? Which formats does it allow to export ?
Sun, 30 Jun 2013 17:25:39 +0200https://ask.sagemath.org/question/10300/compatible-data-file-formats/?answer=15165#post-id-15165Comment by Tharindu Rusira for <p>Note that <code>open()</code> is a Python function that can open <em>any</em> kind of file. The main issue is then to handle your file so that the data can be used in Sage (i.e. transformed into Sage objects).</p>
<p>Since Sage is written in Python, there are many modules to handle many particular types of file (e.g. if you want to handle an xml file, you can imort and use the <code>xml</code> module, if you want to handle a gpg file, you can install, import and use the <code>gnupg</code> module, and so on). If your file is an ad-hoc (non-standard) text file, you can use various python tools to get interesting data from it (e.g. use the <a href="http://docs.python.org/2/library/stdtypes.html#string-methods">string methods</a>, consider also the <a href="http://docs.python.org/2/library/re.html"><code>re</code></a> module allows you to filter your file with regular expressions).</p>
<p>So this really depends on your situation. The data from which sotware do you want to deal with ? Which formats does it allow to export ?</p>
https://ask.sagemath.org/question/10300/compatible-data-file-formats/?comment=17414#post-id-17414Thanks for the answer. Specifically please let me know if it is possible to directly load a spreadsheet in .xls or .ods format. Mon, 01 Jul 2013 02:30:26 +0200https://ask.sagemath.org/question/10300/compatible-data-file-formats/?comment=17414#post-id-17414Comment by tmonteil for <p>Note that <code>open()</code> is a Python function that can open <em>any</em> kind of file. The main issue is then to handle your file so that the data can be used in Sage (i.e. transformed into Sage objects).</p>
<p>Since Sage is written in Python, there are many modules to handle many particular types of file (e.g. if you want to handle an xml file, you can imort and use the <code>xml</code> module, if you want to handle a gpg file, you can install, import and use the <code>gnupg</code> module, and so on). If your file is an ad-hoc (non-standard) text file, you can use various python tools to get interesting data from it (e.g. use the <a href="http://docs.python.org/2/library/stdtypes.html#string-methods">string methods</a>, consider also the <a href="http://docs.python.org/2/library/re.html"><code>re</code></a> module allows you to filter your file with regular expressions).</p>
<p>So this really depends on your situation. The data from which sotware do you want to deal with ? Which formats does it allow to export ?</p>
https://ask.sagemath.org/question/10300/compatible-data-file-formats/?comment=17412#post-id-17412Those formats are dealing with spreadsheet. If you already know how to handle .csv files, i would advise to export your spreadsheet into .csv format and then work with it.
To open an .xls file directly, you can install the `xlrd` module:
From a shell, run the command:
sage -sh
Then, this sage-shell:
(sage-sh) user@machine$ easy_install xlrd
(sage-sh) user@machine$ exit
Then, in Sage command-line or notebook:
sage: import xlrd
For ods files, the `ezodf` seems appropriate, but only works with Python 3 (Sage uses Python 2). You can install and use the `odslib` package (with the same procedure as above).
Mon, 01 Jul 2013 10:02:29 +0200https://ask.sagemath.org/question/10300/compatible-data-file-formats/?comment=17412#post-id-17412