我列了一个id的列表,每个值都是int类型
temp_id = [7922, 7018, 5650, 209, 21928, 2294, 10507, 3623]
type(tempasn)
list
这是有效的:
Temp = pd.read_sql("select count(*) as count\
from "+db+"\
where ids in (7922, 7018, 5650, 209, 21928, 2294, 10507, 3623)\
order by count desc limit 10", conn)
我想把列表中的变量添加到我的查询中
这会导致错误
Temp = pd.read_sql("select count(*) as count\
from "+db+"\
where ids in "+temp_id+"\
order by count desc limit 10", conn)
TypeError: Can't convert 'list' object to str implicitly
我不明白我在这里需要改变什么
回答
最后接受了下面的建议并修改了生成临时ID的循环:
temp_id = []
for id in df['ID']:
if id and id not in temp_id:
temp_id.append(str(ID))
#Convert to list format
temp_id = ", ".join(temp_id)
两件事让这件事成功了:
str(ID) & temp_id = ", ".join(temp_id)