paul100
paul100

Reputation: 148

Convert single quoted elements in list to double quotes using python

Executing this code :

dictt = {'a':1,'b':2,'c':3} 
tuple([i for i in dictt.keys()])

This result is :

['a','b','c']

But I want output to be :

("a","b","c")

The reason behind is i need the output to use it in a SQL QUERY

INSERT INTO TABLE ("a","b","c") VALUES (1,2,3)

Upvotes: 1

Views: 1205

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

I suppose that you want to convert your key list as a string using this format.

A working way would be to use json.dumps which uses double quotes instead of simple quotes when serializing, and replace the enclosing brackets by parentheses (only the first-level ones):

import json

dictt = {'a':1,'b':2,'c':3}

print("({})".format(json.dumps(sorted(dictt.keys()))[1:-1]))

result (as string):

("a", "b", "c")

Upvotes: 1

Related Questions