2

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?

3 Answers 3

2

Use format():

list1 = ('111', '222', '333')
myString = ",".join("'{0}'".format(elem) for elem in list1)
print(myString)

gives

'111','222','333'
Sign up to request clarification or add additional context in comments.

1 Comment

After I obtained a list -- it worked like a charm! :) dataset['LIST'].values.tolist() Thanks alot !
0

Please try this and verify if it helps-

sql = "SELECT * FROM database WHERE list IN (%s)" % ",".join(map(myString,List1))

Comments

0

There is possibility that it will works.

myString = "','".join(List1)
sql = "SELECT * FROM database WHERE list IN ('%s')" %myString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.