Python-从具有识别表达式的字符串创建JSON

时间:2019-05-02 14:56:09

标签: python json string parsing pyparsing

我正在尝试使用带表达式的字符串创建JSON,但在此之前我必须替换操作数。

这是来自用户的输入:

"Apple == 5 & (Plum == 7 | Pear == 8)"

我必须将“ ==”替换为“ eq”或将“&”替换为“ and”等(如果需要,还需要使用更多逻辑表达式)

"Apple eq 5 and (Plum eq 7 or Pear eq 8)"

最后,它应该是 JSON的结果,更精确地说:

{
                "CategoryId": 0,

                "FilterRequest":
                {

                               "Page": 1,

                               "PageSize": 10,

                               "Filter": 
                               { 
                                   "Logic": "and",

                                   "Filters": [
                                       { 
                                           "Logic": "or",
                                           "Filters": [
                                               { 
                                                   "Field": "Plum",
                                                   "Operator": "eq", 
                                                   "Value": "7"
                                               },
                                               { 
                                                   "Field": "Pear",
                                                   "Operator": "eq", 
                                                   "Value": "8"
                                               }
                                           ]
                                       },
                                       { 
                                           "Field": "Apple",
                                           "Operator": "eq", 
                                           "Value": "5"
                                       }
                                   ]
                               }

                }
}

您能告诉我您的想法怎么做吗?也许唯一的办法... 谢谢

已编辑:14/5/2019

我试图找到有关我的问题的尽可能多的信息,我想我已经中途了。如果我选择了正确的方法。 您能给我有关以下代码的反馈或建议吗?

string = "Apple == 5 & (Plum == 7 | Pear == 8)"

string = string.replace('==', ' eq ')
string = string.replace('<>', ' ne ')
string = string.replace('>' , ' gt ')
string = string.replace('>=', ' ge ')
string = string.replace('<' , ' lt ')
string = string.replace('<=', ' le ')
string = string.replace('&' , ' and ')
string = string.replace('|' , ' or ')
string = string.replace('!=', ' not ')

print(string)
# "Apple eq 5 and (Plum eq 7 or Pear eq 8)"


import pyparsing as pp

    operator = pp.Regex(r">=|<=|!=|>|<|=|eq").setName("operator")
    number = pp.Regex(r"[+-]?\d+(:?\.\d*)?(:?[eE][+-]?\d+)?")
    identifier = pp.Word(pp.alphas, pp.alphanums + "_")
    and_ = CaselessLiteral("and").setResultsName("Logic")
    or_ = CaselessLiteral("or").setResultsName("Logic")
    not_ = CaselessLiteral("not").setResultsName("Logic")
    logic = [
            (and_, 2, (pp.opAssoc.LEFT),),
            (or_, 2, pp.opAssoc.LEFT,),
            (not_, 1, pp.opAssoc.RIGHT,),
            ]
    comparison_term = (identifier | number)
    condition = pp.Group(comparison_term("Field") + operator("Operator") + comparison_term("Value"))
    expr = pp.operatorPrecedence(condition("Filters"), logic).setResultsName("Filter")

pars = expr.parseString(string).dump()

import json

    with open("C:\\Users\\palo173\\Desktop\\example.json","w") as f:
        json.dump(o,f)

实际结果,但不幸的是不是最终结果。我想听听您的想法,下一步该怎么做。

{
    "Filter": {
        "Filter": {
            "Filters": [
                {
                    "Field": "Apple",
                    "Operator": "eq",
                    "Value": "5"
                },
                {
                    "Filters": [
                        {
                            "Field": "Plum",
                            "Operator": "eq",
                            "Value": "7"
                        },
                        {
                            "Field": "Pear",
                            "Operator": "eq",
                            "Value": "8"
                        }
                    ],
                    "Logic": "or"
                }
            ],
            "Logic": "and"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议使用

string.replace(old, new, count)
# count is optional, leave it blank like below and it does all occurences

例如

string = "Apple == 5 & (Plum == 7 | Pear == 8)"

string.replace('==', 'eq')
string.replace('&', 'and')
string.replace('|', 'or')

print(string)

>Apple eq 5 and (Plum eq 7 or Pear eq 8
# and so on

现在这仅进行替换,也许使用for循环来迭代字符串列表,但我认为您可以做到。