1

The code and output which I have written is attached below.. FInd it in above images.

    import psycopg2
    conn = psycopg2.connect("dbname='news' user='postgres'")
    cursor = conn.cursor()
    cursor.execute(
        "SELECT * FROM authors")
    results = cursor.fetchall()
    print (results)
    conn.close

The code and output:

The output format what I want:

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • https://stackoverflow.com/questions/10865483/print-results-in-mysql-format-with-python I'd look at the second answer here. – bhow Oct 16 '17 at 19:09

1 Answers1

2

You could use the library pandas for this, it has convenient methods to show data and does the alignment of columns for you.

Here is a small sample:

import pandas as pd
print(pd.DataFrame([(1,2,3), (1,2,3)], columns=['a', 'b', 'c']))

Leads to the output

   a  b  c
0  1  2  3
1  1  2  3

And in your case, you would want to use:

df = pd.DataFrame(results, columns=['name', 'description', 'id'])

I just guessed the column names...

aufziehvogel
  • 7,167
  • 5
  • 34
  • 56