from prettytable import PrettyTable
table = PrettyTable()
table.title = 'Title'
table.field_names = ['Col1', 'Col2']
table.add_row(['Apple Pi', 3.14159])
table.add_row(['Banana', 42])
# Print table to stdout
print(table)
# Save text table to file
with open('output.txt', 'w') as f:
f.write(table.get_string())
$ pip install PTable
PTable is a fork from PrettyTable that allows for titles
To save a PrettyTable to a file, it must be converted to a string first
with open('file.txt', 'w') as f:
f.write(table.get_string())