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:

1

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

3 Comments

Nothing was mentioned about pandas, so why bring it up?
Because it does the job.
You can do the job 100 other ways, too, why not list them all? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.