ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 04 May 2015 08:21:15 -0500How to print a text file containing multiple tuples from a functionhttp://ask.sagemath.org/question/26710/how-to-print-a-text-file-containing-multiple-tuples-from-a-function/ I am trying to use Sagemath Cloud to create a text file with multiple columns of data. Each column is the input or output value of the variables of a system of functions being evaluated with for loops. I am very new to Python, and when I try to do this, I either get an error message or I get a text file with only one line, and that line is constantly overwritten by a new value of the function. On my own laptop with its standalone version of Sage, I can easily generate what I want simply by telling Sage to print the values of the variables of the function for each iteration of the for loops. The standalone version even creates an output text file that I can save. How do I do this with the cloud version?Sun, 03 May 2015 18:42:46 -0500http://ask.sagemath.org/question/26710/how-to-print-a-text-file-containing-multiple-tuples-from-a-function/Answer by calc314 for <p>I am trying to use Sagemath Cloud to create a text file with multiple columns of data. Each column is the input or output value of the variables of a system of functions being evaluated with for loops. I am very new to Python, and when I try to do this, I either get an error message or I get a text file with only one line, and that line is constantly overwritten by a new value of the function. On my own laptop with its standalone version of Sage, I can easily generate what I want simply by telling Sage to print the values of the variables of the function for each iteration of the for loops. The standalone version even creates an output text file that I can save. How do I do this with the cloud version?</p>
http://ask.sagemath.org/question/26710/how-to-print-a-text-file-containing-multiple-tuples-from-a-function/?answer=26712#post-id-26712Here is one possible way to output the results in a csv file.
import csv
# sample function
f(i,j) = i^2*j
with open('tmp.csv', 'w') as f1:
writefile = csv.writer(f1)
for i in range(0,10):
for j in range(0,10):
result=[i,j,f(i,j)]
writefile.writerow(result)
Mon, 04 May 2015 08:21:15 -0500http://ask.sagemath.org/question/26710/how-to-print-a-text-file-containing-multiple-tuples-from-a-function/?answer=26712#post-id-26712