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, 22 Sep 2014 12:30:36 +0200How to save output calculation in 2 columns in simple txt file?https://ask.sagemath.org/question/24219/how-to-save-output-calculation-in-2-columns-in-simple-txt-file/ Dear All,
I am trying to do a simple calculation with sage:
var('x')
for a in [0..10,step=2]:
func=numerical_integral(cos(a*x),0,1)[0]
print a, func
As you can see, the output in sage is written in 2 column. Firs column the number A and the second is the result of calculation:
0 1.0
2 0.454648713413
4 -0.189200623827
6 -0.0465692496998
8 0.123669780828
10 -0.0544021110889
How is it possible to save tow-column result in a simple txt file as it is shown in above? Just number and no any array, no any comma! Just number.
I have came across huge documentation in ask.sagemath.org, Sage manual with such offering such as open("example.txt","w") and even with python commands! No result I could get.
For example:
var('x')
f=open("/tmp/my2.dat", "w")
for a in [0..10,step=2]:
func=numerical_integral(cos(a*x),0,1)[0]
print a, func
f.close()
The question is I don't know how to put PRINT OUTPUT in my2.dat file as two column in above code?
I have read somewhere that it is possible by the following python command:
var('x')
with open("/tmp/my2.dat", "w") as out:
for a in [0..10,step=2]:
func=numerical_integral(cos(a*x),0,1)[0]
print a, func >> out, line
f.close (or implicit_close)
No reslut and with error. Please If some one has a practical suggestion to my first code, let me know about that. I don't want use the commercial MATHEMATICA and so on! I just want to use only SAGE and SAGE!
Thank you.
Sat, 20 Sep 2014 15:27:32 +0200https://ask.sagemath.org/question/24219/how-to-save-output-calculation-in-2-columns-in-simple-txt-file/Answer by ndomes for <p>Dear All,</p>
<p>I am trying to do a simple calculation with sage:</p>
<pre><code>var('x')
for a in [0..10,step=2]:
func=numerical_integral(cos(a*x),0,1)[0]
print a, func
</code></pre>
<p>As you can see, the output in sage is written in 2 column. Firs column the number A and the second is the result of calculation:</p>
<pre><code>0 1.0
2 0.454648713413
4 -0.189200623827
6 -0.0465692496998
8 0.123669780828
10 -0.0544021110889
</code></pre>
<p>How is it possible to save tow-column result in a simple txt file as it is shown in above? Just number and no any array, no any comma! Just number.
I have came across huge documentation in <a href="http://ask.sagemath.org">ask.sagemath.org</a>, Sage manual with such offering such as open("example.txt","w") and even with python commands! No result I could get.
For example:</p>
<pre><code>var('x')
f=open("/tmp/my2.dat", "w")
for a in [0..10,step=2]:
func=numerical_integral(cos(a*x),0,1)[0]
print a, func
f.close()
</code></pre>
<p>The question is I don't know how to put PRINT OUTPUT in my2.dat file as two column in above code?
I have read somewhere that it is possible by the following python command:</p>
<pre><code>var('x')
with open("/tmp/my2.dat", "w") as out:
for a in [0..10,step=2]:
func=numerical_integral(cos(a*x),0,1)[0]
print a, func >> out, line
f.close (or implicit_close)
</code></pre>
<p>No reslut and with error. Please If some one has a practical suggestion to my first code, let me know about that. I don't want use the commercial MATHEMATICA and so on! I just want to use only SAGE and SAGE!</p>
<p>Thank you. </p>
https://ask.sagemath.org/question/24219/how-to-save-output-calculation-in-2-columns-in-simple-txt-file/?answer=24220#post-id-24220Look at Python documentation:
https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
With Sage Notebbook you can do (download the file via Data menu):
f=open(DATA+"mydat.txt", "w")
for a in [0..10,step=2]:
func=numerical_integral(cos(a*x),0,1)[0]
s = " %4i %8.4f \n"%(a,func)
f.write(s)
f.close() Sun, 21 Sep 2014 00:47:43 +0200https://ask.sagemath.org/question/24219/how-to-save-output-calculation-in-2-columns-in-simple-txt-file/?answer=24220#post-id-24220Comment by Kazhal for <p>Look at Python documentation:
<a href="https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files">https://docs.python.org/2/tutorial/in...</a></p>
<p>With Sage Notebbook you can do (download the file via Data menu):</p>
<pre><code>f=open(DATA+"mydat.txt", "w")
for a in [0..10,step=2]:
func=numerical_integral(cos(a*x),0,1)[0]
s = " %4i %8.4f \n"%(a,func)
f.write(s)
f.close()
</code></pre>
https://ask.sagemath.org/question/24219/how-to-save-output-calculation-in-2-columns-in-simple-txt-file/?comment=24241#post-id-24241Thank you for your answer. I have read about that.
But your solution is when I use the graphical interface such as notebook.
I would like to do that in command shell of sage to save file in a directory such
/tmp/ in linux.
I don't know how to do that!
Do you know?Mon, 22 Sep 2014 12:30:36 +0200https://ask.sagemath.org/question/24219/how-to-save-output-calculation-in-2-columns-in-simple-txt-file/?comment=24241#post-id-24241