prettytable.PrettyTable
can represent tabular data in visually appealing formatted tables, and print these tables to the terminal.
How do I save a table as a text file?
Two libraries provide the functionality to pretty print comma-separated values (CSV) in Python: tabulate and prettytable. These don't come standard with Python, so you have to install them with a quick pip install command.
PrettyTable is a Python library that is used to represent tabular data in visually appealing ASCII tables. It is quick and easy to use.
Download the latest version of PrettyTable from the Downloads tab at this Google code project site. Save the file as "prettytable.py" (not prettytable-x.y.py) in your Python installation's "site-packages" directory.
According to the tutorial section on ASCII tables, you can get the table output as a string:
table = ... # code for creating table goes here
table_txt = table.get_string()
with open('output.txt','w') as file:
file.write(table_txt)
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