# How to save output calculation in 2 columns in simple txt file? This post is a wiki. Anyone with karma >750 is welcome to improve it.

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

edit retag close merge delete

Sort by » oldest newest most voted Look at Python documentation: https://docs.python.org/2/tutorial/in...

f=open(DATA+"mydat.txt", "w")
for a in [0..10,step=2]:
func=numerical_integral(cos(a*x),0,1)
s = " %4i  %8.4f \n"%(a,func)
f.write(s)
f.close()

more

Thank 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?