1

From a json file, I have extracted the following:

[u'001', u'002', u'003', u'004', u'005', u'006', u'007', u'009', u'041', 
u'043', u'050', u'099', u'983']

But, what I need is to create a string like this (this will be part of a SQL statement)

str = """not in ('001','002','003','004','005','006','007','009','041','043','050','099','983')"""

I am new to this. Do you have any clues for me? This will be done in Python.

Thank you in advance!

1
  • Please post what you've tried and tell us where you got stuck. Commented Nov 17, 2017 at 20:29

1 Answer 1

3

str.join wrapped in a str.format does the job. First & last quotes and parenthesis are handled by format, whereas middle quotes & commas are handled by str.join

s = [u'001', u'002', u'003', u'004', u'005', u'006', u'007', u'009', u'041',
u'043', u'050', u'099', u'983']

print("not in ('{}')".format("','".join(s)))

result:

not in ('001','002','003','004','005','006','007','009','041','043','050','099','983')

note that str(tuple(s)) generates the same single quoted string, but I don't like relying on the representation of python objects.

Sign up to request clarification or add additional context in comments.

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.