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.Sun, 23 Jul 2017 17:45:39 +0200- Save output as a text file (not in a mathematical sense)https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/How can I save an output directly as a text file (without copy paste)?Fri, 23 Jun 2017 04:52:44 +0200https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/
- Answer by B r u n o for <p>How can I save an output directly as a text file (without copy paste)?</p>
https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?answer=38066#post-id-38066You can use `write`. For instance, the following will create the file `/tmp/file.txt` containing `[1, 2, 3, 4, 5]`.
sage: L = [1,2,3,4,5]
sage: with open("/tmp/file.txt", "w") as f:
....: f.write(str(L))
....:
Converting `L` to its string representation using `str` as above does not guarantee that you'll be able to reconstruct the object afterwards when you read the file. For instance, this is the case if you save a polynomial ring to a file. The solution is to use `dumps` that allows you to recover the object you are saving using `loads`:
sage: R.<x> = ZZ[]
sage: with open("/tmp/file2.txt", "w") as f:
....: f.write(dumps(R))
....:
Then `/tmp/file2.txt` contains a strange-looking string (```xk`J.NLOÕ+ÊÌK/Ö+ÈÏ©ÌËÏÍLÌAbÆäâóóKJKò¸àrA@)®BF
dC2óJRÓSÀÚ¸<!:&Í BæPÆ
¿Î B=```) but you can load the polynomial ring that was saved afterwards:
sage: with open("/tmp/file2.txt") as f:
....: S = loads(f.read())
....:
sage: S
Univariate Polynomial Ring in x over Integer RingFri, 23 Jun 2017 10:59:10 +0200https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?answer=38066#post-id-38066
- Comment by Thrash for <p>You can use <code>write</code>. For instance, the following will create the file <code>/tmp/file.txt</code> containing <code>[1, 2, 3, 4, 5]</code>.</p>
<pre><code>sage: L = [1,2,3,4,5]
sage: with open("/tmp/file.txt", "w") as f:
....: f.write(str(L))
....:
</code></pre>
<p>Converting <code>L</code> to its string representation using <code>str</code> as above does not guarantee that you'll be able to reconstruct the object afterwards when you read the file. For instance, this is the case if you save a polynomial ring to a file. The solution is to use <code>dumps</code> that allows you to recover the object you are saving using <code>loads</code>:</p>
<pre><code>sage: R.<x> = ZZ[]
sage: with open("/tmp/file2.txt", "w") as f:
....: f.write(dumps(R))
....:
</code></pre>
<p>Then <code>/tmp/file2.txt</code> contains a strange-looking string (<code>xk`J.NLOÕ+ÊÌK/Ö+ÈÏ©ÌËÏÍLÌAbÆäâóóKJKò¸àrA@)®BF
dC2óJRÓSÀÚ¸<!:&Í BæPÆ
¿Î B=</code>) but you can load the polynomial ring that was saved afterwards:</p>
<pre><code>sage: with open("/tmp/file2.txt") as f:
....: S = loads(f.read())
....:
sage: S
Univariate Polynomial Ring in x over Integer Ring
</code></pre>
https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38095#post-id-38095And how can I save a text output directly without any converting (executed by Sage)? For example:
sage: ZZ[x]
Univariate Polynomial Ring in x over Integer Ring
How can I save both lines or at the least the second one as a text file? I know this example is not the best one, but when it comes to outputs of thousands of lines (by one command) it would be useful to have the output as a text file because otherwise I would have to scroll manually (for copy paste).Tue, 27 Jun 2017 02:25:35 +0200https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38095#post-id-38095
- Comment by Thrash for <p>You can use <code>write</code>. For instance, the following will create the file <code>/tmp/file.txt</code> containing <code>[1, 2, 3, 4, 5]</code>.</p>
<pre><code>sage: L = [1,2,3,4,5]
sage: with open("/tmp/file.txt", "w") as f:
....: f.write(str(L))
....:
</code></pre>
<p>Converting <code>L</code> to its string representation using <code>str</code> as above does not guarantee that you'll be able to reconstruct the object afterwards when you read the file. For instance, this is the case if you save a polynomial ring to a file. The solution is to use <code>dumps</code> that allows you to recover the object you are saving using <code>loads</code>:</p>
<pre><code>sage: R.<x> = ZZ[]
sage: with open("/tmp/file2.txt", "w") as f:
....: f.write(dumps(R))
....:
</code></pre>
<p>Then <code>/tmp/file2.txt</code> contains a strange-looking string (<code>xk`J.NLOÕ+ÊÌK/Ö+ÈÏ©ÌËÏÍLÌAbÆäâóóKJKò¸àrA@)®BF
dC2óJRÓSÀÚ¸<!:&Í BæPÆ
¿Î B=</code>) but you can load the polynomial ring that was saved afterwards:</p>
<pre><code>sage: with open("/tmp/file2.txt") as f:
....: S = loads(f.read())
....:
sage: S
Univariate Polynomial Ring in x over Integer Ring
</code></pre>
https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38103#post-id-38103Thank you! What do you mean by CO?Tue, 27 Jun 2017 21:19:59 +0200https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38103#post-id-38103
- Comment by dan_fulea for <p>You can use <code>write</code>. For instance, the following will create the file <code>/tmp/file.txt</code> containing <code>[1, 2, 3, 4, 5]</code>.</p>
<pre><code>sage: L = [1,2,3,4,5]
sage: with open("/tmp/file.txt", "w") as f:
....: f.write(str(L))
....:
</code></pre>
<p>Converting <code>L</code> to its string representation using <code>str</code> as above does not guarantee that you'll be able to reconstruct the object afterwards when you read the file. For instance, this is the case if you save a polynomial ring to a file. The solution is to use <code>dumps</code> that allows you to recover the object you are saving using <code>loads</code>:</p>
<pre><code>sage: R.<x> = ZZ[]
sage: with open("/tmp/file2.txt", "w") as f:
....: f.write(dumps(R))
....:
</code></pre>
<p>Then <code>/tmp/file2.txt</code> contains a strange-looking string (<code>xk`J.NLOÕ+ÊÌK/Ö+ÈÏ©ÌËÏÍLÌAbÆäâóóKJKò¸àrA@)®BF
dC2óJRÓSÀÚ¸<!:&Í BæPÆ
¿Î B=</code>) but you can load the polynomial ring that was saved afterwards:</p>
<pre><code>sage: with open("/tmp/file2.txt") as f:
....: S = loads(f.read())
....:
sage: S
Univariate Polynomial Ring in x over Integer Ring
</code></pre>
https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38099#post-id-38099The linux `script` command?
Before starting sage, start the script. Then everything shown in the console is also written in the file mentioned to script. Then quit sage and the script. This is a valid protocol of the session, using some text editor one can search+edit relevant lines.
Also...
[dan@f ~]$ script -c "sage -c \"E = EllipticCurve( QQ, [-65^2,0] ); print E, 'RANK =', E.rank()\"" a.log
Script started, file is a.log
Elliptic Curve defined by y^2 = x^3 - 4225*x over Rational Field RANK = 2
Script done, file is a.log
[dan@f ~]$ cat a.log
Script started on Di 27 Jun 2017 18:44:47 CEST
Elliptic Curve defined by y^2 = x^3 - 4225*x over Rational Field RANK = 2
Script done on Di 27 Jun 2017 18:44:50 CEST
[dan@f ~]$
& CO is possible.Tue, 27 Jun 2017 18:50:26 +0200https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38099#post-id-38099
- Comment by ortollj for <p>You can use <code>write</code>. For instance, the following will create the file <code>/tmp/file.txt</code> containing <code>[1, 2, 3, 4, 5]</code>.</p>
<pre><code>sage: L = [1,2,3,4,5]
sage: with open("/tmp/file.txt", "w") as f:
....: f.write(str(L))
....:
</code></pre>
<p>Converting <code>L</code> to its string representation using <code>str</code> as above does not guarantee that you'll be able to reconstruct the object afterwards when you read the file. For instance, this is the case if you save a polynomial ring to a file. The solution is to use <code>dumps</code> that allows you to recover the object you are saving using <code>loads</code>:</p>
<pre><code>sage: R.<x> = ZZ[]
sage: with open("/tmp/file2.txt", "w") as f:
....: f.write(dumps(R))
....:
</code></pre>
<p>Then <code>/tmp/file2.txt</code> contains a strange-looking string (<code>xk`J.NLOÕ+ÊÌK/Ö+ÈÏ©ÌËÏÍLÌAbÆäâóóKJKò¸àrA@)®BF
dC2óJRÓSÀÚ¸<!:&Í BæPÆ
¿Î B=</code>) but you can load the polynomial ring that was saved afterwards:</p>
<pre><code>sage: with open("/tmp/file2.txt") as f:
....: S = loads(f.read())
....:
sage: S
Univariate Polynomial Ring in x over Integer Ring
</code></pre>
https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38162#post-id-38162Hi
I would like to do that with downloading a file example.sage
content of example.sage:
print("Hello World")
but that does not work ;-(
script -c "sage -c \"load("example.sage") \"" a.log
I suppose the syntax should be adapted for that ?
sage: cat a.log
Script started on Sun 02 Jul 2017 02:23:55 AM EDT
Traceback (most recent call last):
File "/home/sage/sage-7.6/src/bin/sage-eval", line 10, in <module>
eval(compile(s,'<cmdline>','exec'))
File "<cmdline>", line 1, in <module>
NameError: name 'example' is not defined
Script done on Sun 02 Jul 2017 02:23:59 AM EDT
sage:Sun, 02 Jul 2017 08:40:07 +0200https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38162#post-id-38162
- Comment by dan_fulea for <p>You can use <code>write</code>. For instance, the following will create the file <code>/tmp/file.txt</code> containing <code>[1, 2, 3, 4, 5]</code>.</p>
<pre><code>sage: L = [1,2,3,4,5]
sage: with open("/tmp/file.txt", "w") as f:
....: f.write(str(L))
....:
</code></pre>
<p>Converting <code>L</code> to its string representation using <code>str</code> as above does not guarantee that you'll be able to reconstruct the object afterwards when you read the file. For instance, this is the case if you save a polynomial ring to a file. The solution is to use <code>dumps</code> that allows you to recover the object you are saving using <code>loads</code>:</p>
<pre><code>sage: R.<x> = ZZ[]
sage: with open("/tmp/file2.txt", "w") as f:
....: f.write(dumps(R))
....:
</code></pre>
<p>Then <code>/tmp/file2.txt</code> contains a strange-looking string (<code>xk`J.NLOÕ+ÊÌK/Ö+ÈÏ©ÌËÏÍLÌAbÆäâóóKJKò¸àrA@)®BF
dC2óJRÓSÀÚ¸<!:&Í BæPÆ
¿Î B=</code>) but you can load the polynomial ring that was saved afterwards:</p>
<pre><code>sage: with open("/tmp/file2.txt") as f:
....: S = loads(f.read())
....:
sage: S
Univariate Polynomial Ring in x over Integer Ring
</code></pre>
https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38368#post-id-38368The inner `"example.sage"` can be changed into `'example.sage'` to avoid the parsing yoga of `sage -c`. For instance:
[dan@f ~/tmp]$ echo "E = EllipticCurve( QQ, [-65**2,0] ); print E, 'RANK =', E.rank()" > example.sage
[dan@f ~/tmp]$ script -c "sage -c \"load( 'example.sage' ) \"" a.log
Script started, file is a.log
Elliptic Curve defined by y^2 = x^3 - 4225*x over Rational Field RANK = 2
Script done, file is a.log
[dan@f ~/tmp]$ cat a.log
Script started on So 23 Jul 2017 17:42:51 CEST
Elliptic Curve defined by y^2 = x^3 - 4225*x over Rational Field RANK = 2
Script done on So 23 Jul 2017 17:42:54 CESTSun, 23 Jul 2017 17:45:39 +0200https://ask.sagemath.org/question/38063/save-output-as-a-text-file-not-in-a-mathematical-sense/?comment=38368#post-id-38368