Skip to main content
 首页 » 编程设计

查询中的 Python 列表

2025年05月04日19cmt

我正在使用 python 插件。我使用列表来存储一些值,如下所示:

known_stn.append('1') 
known_stn.append('2') 

我的查询是

query=("SELECT survey, station FROM stat WHERE stat.station IN (%s) AND station.survey = '2011410'" %known_stn) 

WHERE station.station IN (['1', '2']) 发生错误,因为列表包含 [] 括号。 我尝试更换这些支架,但没有更换。

还有其他数据结构可以用吗??或者替换方括号的方法...

请您参考如下方法:

您需要先将列表转换为字符串:

>>> my_list = [1,2,3] 
>>> str(my_list) 
'[1, 2, 3]' 
>>> map(str, my_list) 
['1', '2', '3'] 
>>> ','.join(map(str, my_list)) 
'1,2,3' 
>>> 'select ... where foo in (%s)' % ','.join(map(str, my_list)) 
'select ... where foo in (1,2,3)'