I am working with a psql database and python, and I need a way to convert each element of a record array into a variable attached to an empty list. I also need to avoid global variables if at all possible. The overall goal is to create a variable with the name of a table, and set the variable equal to a list of column headers. I was able to pull a record array containing the table names from the database, with
tables= db.query2rec("""select table_name from information_schema.tables where table_schema = 'public';""")
where db and query2rec use some psycopg2 stuff to give me
tables= rec.array([('table1',), ('table2',), ('table3',), ('table4',), ('table5',)],
dtype=[('table_name', 'O')])
I need to get this in the form of
table1 = []
table2 = []
table3 = []
table4 = []
table5 = []
I can then use a second query to populate each of these with a list of headers from each table. My biggest challenge is getting the elements of the record array as variable names. I have no clue how to do it, although I have spent a few hours looking at everything from pickling and dictionaries to for loops and global variables. Thanks!