Python 拆分字符串以创建SQL筛选器
您好,我有一个列表项,其字符串如下:Python 拆分字符串以创建SQL筛选器,python,sql,sqlalchemy,Python,Sql,Sqlalchemy,您好,我有一个列表项,其字符串如下: or_args = [and_( table1.c.columnvalue1 == scope[0], table1.c.columnvalue12.in_(scope[1:])) for scope in scope_list] 范围项目=[“4760 5.2 5.1,acs 8.0.0 7.1,芝加哥4.1.0 4.0.0 3.2.0 1.0.1 3.0.0”] 我在一个名为scope\u list scope_list = ['4760', '5.
or_args = [and_(
table1.c.columnvalue1 == scope[0],
table1.c.columnvalue12.in_(scope[1:])) for scope in scope_list]
范围项目=[“4760 5.2 5.1,acs 8.0.0 7.1,芝加哥4.1.0 4.0.0 3.2.0 1.0.1 3.0.0”]
我在一个名为scope\u list
scope_list = ['4760', '5.2', '5.1,', 'acs', '8.0.0', '7.1,', 'chicago', '4.1.0', '4.0.0', '3.2.0', '1.0.1', '3.0.0']
但正如您所看到的,逗号(,
)也被存储了
我想创建一个SQL语句,如:
WHERE (
columnvalue1 = 4760
AND columnvalue2 IN (5.2,5.1)
)
OR (
columnvalue1="acs"
AND columnvalue2 IN (8.0.0,7.1)
)
OR (
columnvalue1 = "chicago"
AND colmnvalue2 IN (4.1.0, 4.0.0, 3.2.0, 1.0.1, 3.0.0)
)
我使用的是SQLAlchemy,我管理的第一部分(在OR之前)如下:
or_args = [and_(
table1.c.columnvalue1 == scope[0],
table1.c.columnvalue12.in_(scope[1:])) for scope in scope_list]
如何对每个
,
之后的所有其他值执行相同的操作?您可以在逗号上拆分:
scope_groups = scope_items[0].split(", ")
现在,scope\u groups
中的每个元素都是规则的一个组,其中第0项是列,任何其他项都是值:
for group in scope_groups:
group = group.split(" ")
# use group[0] and group[1:]