params.sort(key=lambda k_v: len(str(k_v[0])), reverse=True)
for k, v in params:
    """Some types don't need escaping"""
if isinstance(v, (int, float, bool)):
    v = str(v)
else:
    v = "'%s'" % v
"""Replace the placeholders with values
    Works both with :1 and %(foo)s type placeholders"""
query = query.replace(':%s' % k, v)
query = query.replace('%%(%s)s' % k, v)
How to use SQLAlchemy to dump an SQL file from query expressions to bulk-insert into a DBMS?