Ask Your Question
1

Save output as a text file (not in a mathematical sense)

asked 2017-06-22 21:52:44 -0500

Tommy Angelo gravatar image

updated 2017-06-27 10:19:47 -0500

How can I save an output directly as a text file (without copy paste)?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
3

answered 2017-06-23 03:59:10 -0500

B r u n o gravatar image

You 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 (xœk`J.NLOÕ+ÊÌK/Ö+ÈÏ©ÌËÏÍLÌAbƃäâ“óóŠKŠJ“Kò‹¸à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 Ring
edit flag offensive delete link more

Comments

And 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).

Tommy Angelo gravatar imageTommy Angelo ( 2017-06-26 19:25:35 -0500 )edit
2

The 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.

dan_fulea gravatar imagedan_fulea ( 2017-06-27 11:50:26 -0500 )edit

Thank you! What do you mean by CO?

Tommy Angelo gravatar imageTommy Angelo ( 2017-06-27 14:19:59 -0500 )edit

Hi 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:
ortollj gravatar imageortollj ( 2017-07-02 01:40:07 -0500 )edit

The 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 CEST
dan_fulea gravatar imagedan_fulea ( 2017-07-23 10:45:39 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2017-06-22 21:52:44 -0500

Seen: 96 times

Last updated: Jun 27