How to output from sage (update)

asked 2022-12-13 21:00:45 +0100

c05772 gravatar image

updated 2022-12-13 21:14:07 +0100

This question was answered there 12 years ago. I was using the code from ccanonc

with open("outputfilepath", 'w') as > fp:
>     for line in iterable: #assuming line is str with \n
>         print >> fp, line
> #implicit fp.close() here as "with" scope endsto

today with python 3 and sage 9.5, I get: NameError: name 'iterable' is not defined

What is the easiest way to update this code?

Note that the line

print >> fp, line

is converted in

print(line, file=fp)
edit retag flag offensive close merge delete



The old answer assumes that you are writing information from an "iterable": a Python list or tuple or similar object. So replace "iterable" with the name of the list storing the information. If you want to write a single object X, not the entries of a list, just skip the for loop and print(X, file=fp). This is not specific to Sage: any information on the internet about writing to a file with Python will be relevant.

John Palmieri gravatar imageJohn Palmieri ( 2022-12-14 01:27:29 +0100 )edit