py解析输入和特定输出

2024-04-24 07:25:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在考虑如何解析以下输入:

comment ='  @Class wordinfo dict<<position:int>,wordinfo:str>\n ' + \
                           '@Class instances dict<<word:str>,instances:atomicint> '

对于特定输出:

{'wordinfo': {'columns': [('wordinfo', 'text')],
              'primary_keys': [('position', 'int')],
              'type': 'StorageDict'},

 'instances': {'columns': [('instances', 'counter')],
               'primary_keys': [('word', 'text')],
               'type': 'StorageDict'}
}

正如我们在上面看到的,我需要把字典的键作为主键,然后我可以有一个或多个值作为列,首先我总是有变量名,然后是变量类型,我问自己是否有一些基本的方法来得到我想要的结果,因为我不是pyparsing的专家。是否可行?我需要做些什么


Tags: columnsinstancestexttypecommentpositionkeysdict
1条回答
网友
1楼 · 发布于 2024-04-24 07:25:36

第一步是编写一个BNF。你已经开始这样想了,当你写:我需要把字典的键作为主键,然后我可以有一个或多个值作为列,首先我总是有变量名,然后是变量类型

将此转换为更正式的内容:

class_definition :: '@Class' identifier class_body
class_body :: class_dict  // can add other types here as necessary
class_dict :: 'dict' '<' '<' identifier ':' value_type '>' ','
                     column_decl [',' column_decl]... '>'
column_decl :: identifier ':' value_type
value_type :: 'int' | 'str' | 'atomicint'

嗯,identifier : value_type在几个地方,让我们称之为var_decl并重写。另外,我认为通过在<>中定义一个逗号分隔的列表,您有可能拥有复合主键,我们在几个地方使用这种列表。重写:

class_definition :: '@Class' identifier class_body
class_body :: class_dict  // can add other types here as necessary
class_dict :: 'dict' '<' '<' vars_decl '>' ',' vars_decl '>'
vars_decl :: var_decl [',' var_decl]...
var_decl :: identifier ':' value_type
value_type :: 'int' | 'str' | 'atomicint'

然后自下而上用解析术语定义这些:

import pyparsing as pp
S = pp.Suppress
identifier = pp.pyparsing_common.identifier
value_type = pp.oneOf("int str atomicint")
var_decl = pp.Group(identifier + S(":") + value_type)
vars_decl = pp.Group(pp.delimitedList(var_decl))
dict_decl = pp.Group(S("dict") + S("<") 
                     + S("<") + vars_decl + S(">") + S(",")
                     + vars_decl 
                     + S(">"))
class_decl = pp.Group('@Class' + identifier + dict_decl)

最后,输入结果名称,以便在解析后更容易地挑出不同的部分:

import pyparsing as pp
S = pp.Suppress
identifier = pp.pyparsing_common.identifier
value_type = pp.oneOf("int str atomicint")
var_decl = pp.Group(identifier("name") + S(":") + value_type("type"))
vars_decl = pp.Group(pp.delimitedList(var_decl))
dict_decl = pp.Group(S("dict") + S("<") 
                     + S("<") + vars_decl("primary_key") + S(">") + S(",")
                     + vars_decl("columns") 
                     + S(">"))
class_decl = pp.Group('@Class'
                      + identifier("class_name")
                      + dict_decl("class_body"))

然后使用以下方法分析文本:

class_definitions = pp.OneOrMore(class_decl).parseString(comment)

打印出你得到的信息:

print(class_definitions.dump())

或者更好:

class_decl.runTests(comment)

这是完全未经检验,可能是一个不匹配的帕伦在那里,但这是一般的想法。但即使最后使用的不是pyparsing,也要从BNF开始。这将真正有助于澄清你的想法,和一般概念的问题

相关问题 更多 >