Print tab separated values as table using MySQL
Using mysql command line utility to get recordsets, the data rows alignment and line breaks are often a mess. You can use the command line tool on a mysql database server to get a set of rows into an output TSV file like so:shell> mysql -u your_user -p < your_statement.sql > data.csv
I came up with the following python script to grab the output file and pretty print:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from tabulate import tabulate | |
if __name__ == '__main__': | |
report_headers = [] | |
data = [] | |
with open('data.csv','r') as f: | |
reader=csv.reader(f,delimiter='\t') | |
for dataRow in reader: | |
data.append(dataRow) | |
report_headers = data[0] | |
del data[0] | |
print tabulate(data,report_headers) |
https://pypi.python.org/pypi/tabulate
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment