문제

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?

도움이 되었습니까?

해결책

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')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top