我有这个功能:
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)