Reputation: 315
Goal: how to convert (111, 222, 333) to ('111', '222', '333') for an sql query in Python?
What I have done so far:
I am calling a csv file to a df:
dataset = pd.read_csv('simple.csv')
print(dataset)
LIST 0 111 1 222 2 333
List11 = dataset.LIST.apply(str)
print(List1)
0 111 1 222 2 333
Name: OPERATION, dtype: object
myString = ",".join(List1)
print(myString)
111,222,333
sql = "SELECT * FROM database WHERE list IN (%s)" %myString
This does not work. Could you please help?
Upvotes: 2
Views: 1923
Reputation: 219
There is possibility that it will works.
myString = "','".join(List1)
sql = "SELECT * FROM database WHERE list IN ('%s')" %myString
Upvotes: 0
Reputation: 121654
Use format()
:
list1 = ('111', '222', '333')
myString = ",".join("'{0}'".format(elem) for elem in list1)
print(myString)
gives
'111','222','333'
Upvotes: 2
Reputation: 445
Please try this and verify if it helps-
sql = "SELECT * FROM database WHERE list IN (%s)" % ",".join(map(myString,List1))
Upvotes: 0