I have a neat little table using tabulate:
from tabulate import tabulate
outputList = dictOfOutputs.items()
table = outputList
print tabulate(table)
How do I print this to a text file?
Just write it how you normally would write a string to a file:
with open('table.txt', 'w') as f:
f.write(tabulate(table))
The tabulate() function returns a string; just write it to a file:
with open('filename.txt', 'w') as outputfile:
outputfile.write(tabulate(table))
You can always make print output to a file instead of sys.stdout by using >> redirection:
with open('filename.txt', 'w') as outputfile:
print >> outputfile, tabulate(table)
or by using the print() function (put from __future__ import print_function at the top of your module if you are using Python 2):
from __future__ import print_function
with open('filename.txt', 'w') as outputfile:
print(tabulate(table), file=outputfile)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With