[英]How to parse custom file format to json in Python or to Python dictionary?
我有我在 Python 中用于我的程序的自定义文件格式。它以人类可读的形式存储逻辑门。
[output-0(
or-0(
and-0(
not-0(
input-0()),
not-1(
input-1())),
and-2(
input-0(),
not-1(
input-1())))),
output-1(
or-1(
and-1(
not-0(
input-0()),
input-1()),
and-2(
input-0(),
not-1(
input-1()))))]
我想将其解析为 json,如下所示:
[
{
"type": "output",
"id": 0,
"i": [
{
"type": "or",
"id": 0,
"i": [
{
"type": "and",
"id": 0,
"i": [
{
"type": "not",
"id": 0,
"i": [
{
"type": "input",
"id": 0,
"i": []
}
]
},
{
"type": "not",
"id": 1,
"i": [
{
"type": "input",
"id": 1,
"i": []
}
]
}
]
},
{
"type": "and",
"id": 2,
"i": [
{
"type": "input",
"id": 0,
"i": []
},
{
"type": "not",
"id": 1,
"i": [
{
"type": "input",
"id": 1,
"i": []
}
]
}
]
}
]
}
]
},
{
"type": "output",
"id": 1,
"i": [
{
"type": "or",
"id": 1,
"i": [
{
"type": "and",
"id": 1,
"i": [
{
"type": "not",
"id": 0,
"i": [
{
"type": "input",
"id": 0,
"i": []
}
]
},
{
"type": "input",
"id": 1,
"i": []
}
]
},
{
"type": "and",
"id": 2,
"i": [
{
"type": "input",
"id": 0,
"i": []
},
{
"type": "not",
"id": 1,
"i": [
{
"type": "input",
"id": 1,
"i": []
}
]
}
]
}
]
}
]
}
]
或者如果可以不用json直接转成python字典。所有的门类型都是:and,or,not,buffer,nand,nor,xor,output,input。 谢谢你的帮助。
定义语法:
type := word
id := number
func := type-id
call := func(args)
args := none | call {, call}*
root := [args]
让我们使用pyparsing
使自己成为一个解析器:
import json
import pyparsing as pp
def make_parser():
LPAR, RPAR = map(pp.Suppress, "()")
LBRA, RBRA = map(pp.Suppress, "[]")
type_ = pp.Word(pp.alphas)
id_ = pp.Word(pp.nums)
func = type_ + "-" + id_
call = pp.Forward()
args = pp.Optional(call + pp.ZeroOrMore("," + call))
call <<= func + pp.Group(LPAR + args + RPAR)
many = args
root = pp.Group(LBRA + many + RBRA)
id_.setParseAction(parse_id)
args.setParseAction(parse_args)
call.setParseAction(parse_call)
root.setParseAction(parse_root)
return root
定义将令牌转换为 JSON 格式的操作:
def parse_id(s, locs, tokens):
opand, = tokens
return int(opand)
def parse_call(s, locs, tokens):
type_, _dash, id_, args = tokens
return {"type": type_, "id": id_, "i": list(args)}
def parse_args(s, locs, tokens):
return tokens[::2]
def parse_root(s, locs, tokens):
root, = tokens
return root
跑步:
test = "[output-0()]"
parser = make_parser()
result = parser.parseString(test)[:]
print(json.dumps(result, indent=4))
完整示例:
import json
import pyparsing as pp
def parse_id(s, locs, tokens):
opand, = tokens
return int(opand)
def parse_call(s, locs, tokens):
type_, _dash, id_, args = tokens
return {"type": type_, "id": id_, "i": list(args)}
def parse_args(s, locs, tokens):
return tokens[::2]
def parse_root(s, locs, tokens):
root, = tokens
return root
def make_parser():
LPAR, RPAR = map(pp.Suppress, "()")
LBRA, RBRA = map(pp.Suppress, "[]")
type_ = pp.Word(pp.alphas)
id_ = pp.Word(pp.nums)
func = type_ + "-" + id_
call = pp.Forward()
args = pp.Optional(call + pp.ZeroOrMore("," + call))
call <<= func + pp.Group(LPAR + args + RPAR)
many = args
root = pp.Group(LBRA + many + RBRA)
id_.setParseAction(parse_id)
args.setParseAction(parse_args)
call.setParseAction(parse_call)
root.setParseAction(parse_root)
return root
def main():
test = """[
output-0(
or-0(
and-0(
not-0(
input-0()),
not-1(
input-1())),
and-2(
input-0(),
not-1(
input-1())))),
output-1(
or-1(
and-1(
not-0(
input-0()),
input-1()),
and-2(
input-0(),
not-1(
input-1()))))
]"""
parser = make_parser()
result = parser.parseString(test)[:]
print(json.dumps(result, indent=4))
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.