Skip to content

Instantly share code, notes, and snippets.

@siennathesane
Created June 25, 2015 21:30
Show Gist options
  • Save siennathesane/bc3918c0328057bdcc71 to your computer and use it in GitHub Desktop.
Save siennathesane/bc3918c0328057bdcc71 to your computer and use it in GitHub Desktop.
from sqlalchemy import create_engine, MetaData, Table, select, func
engine = create_engine('postgresql+psycopg2://postgres:postgres@localhost:5432/cm', convert_unicode=True)
metadata = MetaData(bind=engine)
db = engine.connect()
hosts = Table('hosts', metadata, autoload=True)
results = db.execute(select({hosts}))
json_results = json.dumps([dict(r) for r in results])
results = db.execute(select({hosts}))
hosts_list =[]
for x in results:
hosts_list.append(x[hosts.c.host_name])
hosts_dict = dict.fromkeys(hosts_list)
for key, value in hosts_dict.items():
hosts_dict[key] = {
{column : value for column,value in x.items()}
for x in results}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment