gpt4 book ai didi

python - 使用 python 将列表中的单引号元素转换为双引号

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:16 32 4
gpt4 key购买 nike

执行此代码:

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

这个结果是:

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

但我希望输出是:

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

背后的原因是我需要输出才能在 SQL 查询中使用它

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

最佳答案

我想您想使用这种格式将 key 列表转换为字符串。

一种工作方法是使用 json.dumps ,它在序列化时使用双引号而不是单引号,并用括号替换括号(仅第一级括号):

import json

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

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

结果(作为字符串):

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

关于python - 使用 python 将列表中的单引号元素转换为双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43206485/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com