Ask Your Question

Moving a shift entered result as a code in another file

asked 2021-04-14 07:08:38 +0100

whatupmatt gravatar image

updated 2021-04-14 08:37:23 +0100

slelievre gravatar image

So suppose I did tons of code and end up with this result.

C:\fakepath\Screenshot (82).png

I want to use this result in a new code, say another new file. If I copy and paste this, it would not work as I would have to manually put parenthesis around each exponent. Is there some way to convert this output to code (basically the polynomials with the parenthesis on the exponents) and transport it to another file?

edit retag flag offensive close merge delete


If you capture only the useful zone, or crop to remove the grey area on the sides, the picture can be displayed bigger.

Even better, copy and paste some input and/or output and apply code formatting to it (select code and press ctrl-K or cmd-K, or indent by 4 spaces, or use the "101 010" button).

slelievre gravatar imageslelievre ( 2021-04-14 08:50:31 +0100 )edit

Also, reducing to a simpler case to get a simpler question is always a good idea.

slelievre gravatar imageslelievre ( 2021-04-14 08:51:27 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-04-14 08:50:21 +0100

slelievre gravatar image

updated 2021-04-14 08:55:26 +0100

By "this result" I assume you mean the one numbered Out[14] in the screen capture.

There is no need for any extra parentheses around exponents.

Copying and pasting this to a new file should work.

Or you could give the result a name and save it to a "sage object" file.

my_polynomial_tuple = ..., ..., ...
save(my_polynomial_tuple, 'my_polynomials.sobj')

Then read it from a different file

my polynomial_tuple = load('my_polynomials.sobj')

Or you could write the output to a file as a string, and then read it back from that file, using Python functionality.

Something like the following should work.


with open('my_polynomials.txt', 'w') as f:


P = PolynomialRing(QQ, ['w', 'x', 'y', 'z'])

with open('my_polynomials.txt', 'r') as f:
    s =

polynomials_as_strings = s[1:-1].split(',')
polynomials = [P(p) for p in polynomials_as_strings]
edit flag offensive delete link more


Great Thanks.

whatupmatt gravatar imagewhatupmatt ( 2021-04-17 19:00:55 +0100 )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

1 follower


Asked: 2021-04-14 07:08:38 +0100

Seen: 113 times

Last updated: Apr 14 '21