First time here? Check out the FAQ!

Ask Your Question
1

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

asked 7 years ago

Thrash gravatar image

updated 7 years ago

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

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
3

answered 7 years ago

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
Preview: (hide)
link

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

Thrash gravatar imageThrash ( 7 years ago )
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 ( 7 years ago )

Thank you! What do you mean by CO?

Thrash gravatar imageThrash ( 7 years ago )

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 ( 7 years ago )

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 ( 7 years ago )

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: 7 years ago

Seen: 2,992 times

Last updated: Jun 27 '17