小编Nyl*_*ius的帖子

Python递归函数或循环将字符串转换为json逻辑对象

我有这个功能:

def req_splitter(req_string):
    req = {}
    if " AND " in req_string:
        cond = "AND"
        req_splitted = req_string.split(" AND ")
    elif " OR " in req_string:
        cond = "OR"
        req_splitted = req_string.split(" OR ")
    else:
        cond = "AND"
        req_splitted = [req_string]

    if len(req_splitted) > 1:
        for sub_req in req_splitted:
            sub_req_splitted = req_splitter(sub_req)
            req[cond] = list()#new_req
            req[cond].append(sub_req_splitted)
    else:
        req[cond] = req_splitted
    return req
Run Code Online (Sandbox Code Playgroud)

它旨在将像这样的字符串转换为json逻辑条件:

Barracks AND Tech Lab
Lair OR Hive
Hatchery OR Lair OR Hive
Cybernetics Core AND Gateway OR …
Run Code Online (Sandbox Code Playgroud)

python recursion logic json polish-notation

1
推荐指数
1
解决办法
552
查看次数

标签 统计

json ×1

logic ×1

polish-notation ×1

python ×1

recursion ×1