Python 如何将列表变量从元组转换为字符串
我得到以下错误: 索引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','Misc',]Python 如何将列表变量从元组转换为字符串,python,python-3.x,sqlite,Python,Python 3.x,Sqlite,我得到以下错误: 索引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','Misc',] def stainless_list(self): stainle
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)
致:
您的列表是一个元组列表,其中每个元组都是一行数据。可能令人困惑的是,select的输出是一个行列表,每行中只有一个值。试试这个:
for row in res:
stainless_getlist.append( row[0] )
第一行是包含一个字符串“304”的元组,而不仅仅是一个字符串“304”
for row in res:
stainless_getlist.append( row[0] )