Question

In python, I have the following sql query:

qry = "insert into golden_table (alpha, beta, week1, week2, week3, clust_list, nGram) values (%s %s %s %s %s %s %s)"

Now I wish to do something like:

L = ['a', 'b', 'c']
qry % (1, 2, UNPACK(L), "herp", "derp")

The result would be:

"insert into golden_table (alpha, beta, week1, week2, week3, clust_list, nGram) values (1, 2, 'a', 'b', 'c', "herp", "derp")"

How can I accomplish this?

Was it helpful?

Solution

If you want to build tuples, you can use tuple(L) to turn a list into a tuple, and combine tuples with +.

Ex:

>>> L
['a', 'b', 'c']
>>> (1,) + (2,) + tuple(L) + ("herp", "derp")
(1, 2, 'a', 'b', 'c', 'herp', 'derp')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top