简体   繁体   中英

Create a concatenated string from json object in Python

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM