Skip to Content

traitement comparable avec la fonction toDict (universel)

  1. ## fonction toDict: Recipe 528939 - Converting DBI results to a list of dictionaries
  2. def toDict(curs):
  3. cols = [column[0] for column in curs.description]
  4. return [dict(zip(cols, row)) for row in curs.fetchall()]
  5.  
  6.  
  7. cur = conn.cursor()
  8. query = "SELECT nom, AsText(the_geom), Srid(the_geom) FROM testpoly where nom='%s'" % 'test1'
  9. cur.execute(query)
  10. print toDict(cur)
  11. [{'srid': 4326, 'nom': 'test1', 'astext': 'POLYGON((0.09094470046083 0.807557603686636,0.141635944700461 0.80147465437788,0.360622119815668 0.702119815668203,0.240990783410138 0.548018433179723,0.086889400921659 0.584516129032258,0.054447004608295 0.742672811059908,0.09094470046083 0.807557603686636))'}]