How do I get a list of column names from a psycopg2 cursor?

前端 未结 10 2079
清歌不尽
清歌不尽 2020-12-12 11:49

I would like a general way to generate column labels directly from the selected column names, and recall seeing that python\'s psycopg2 module supports this feature.

10条回答
  •  情深已故
    2020-12-12 12:29

    I also used to face similar issue. I use a simple trick to solve this. Suppose you have column names in a list like

    col_name = ['a', 'b', 'c']
    

    Then you can do following

    for row in cursor.fetchone():
        print zip(col_name, row)
    

提交回复
热议问题