gpt4 book ai didi

python - 如何将我的列表变量从元组转换为字符串

转载 作者:行者123 更新时间:2023-11-28 22:12:10 25 4
gpt4 key购买 nike

我收到以下错误:

索引 0 的类型为“tuple”,但应为“str”

我的列表变量如下所示:

[('304',), ('316',), ('303',), ('BS S130',), ('请勿使用',), ('17-4PH',) , ('431S29',), ('416',), ('304',), ('316',), ('S143D',), ('15/5PH-H1025',), ('304S11 ',), ('316S11',), ('304L',), ('316L',), ('304S16',), ('BS S527',), ('316L',), ('316 ',), ('杂项',)]

def stainless_list(self):
stainless_getlist = []

content = 'SELECT grade FROM stainless ORDER BY prefix ASC'
res = conn.execute(content)
for row_index, row_data in enumerate(res):
stainless_getlist.append(row_data)

conn.close

self.comboBox_2.clear()
self.comboBox_2.addItems(stainless_getlist)

print(stainless_getlist)
return

如何将我的列表声明为字符串而不是元组,以便它正确附加到我的组合框?

最佳答案

您可以在遍历行时解压缩每个元组的第一项(并且您不需要枚举,因为您没有使用行索引)。

改变:

for row_index, row_data in enumerate(res):
stainless_getlist.append(row_data)

到:

for item, in res:
stainless_getlist.append(item)

关于python - 如何将我的列表变量从元组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55192076/

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