首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在吡咯烷酮分析中,如何分配“无匹配”键值?

在吡咯烷酮分析中,如何分配“无匹配”键值?
EN

Stack Overflow用户
提问于 2013-10-12 19:06:44
回答 2查看 456关注 0票数 3

我想使'pyparsing‘解析结果作为字典出来,而不需要后置处理。为此,我需要定义自己的键字符串。以下是我能想出的最好的结果。

行来解析:

代码语言:javascript
复制
%ADD22C,0.35X*%

代码:

代码语言:javascript
复制
import pyparsing as pyp

floatnum = pyp.Regex(r'([\d\.]+)')
comma = pyp.Literal(',').suppress()

cmd_app_def = pyp.Literal('AD').setParseAction(pyp.replaceWith('aperture-definition'))

cmd_app_def_opt_circ = pyp.Group(pyp.Literal('C') +
comma).setParseAction(pyp.replaceWith('circle'))

circular_apperture = pyp.Group(cmd_app_def_opt_circ +
pyp.Group(pyp.Empty().setParseAction(pyp.replaceWith('diameter')) + floatnum) +
pyp.Literal('X').suppress())

<the grammar for the entire line>

结果是:

代码语言:javascript
复制
['aperture-definition', '20', ['circle', ['diameter', '0.35']]]

我认为这里的黑客

代码语言:javascript
复制
pyp.Empty().setParseAction(pyp.replaceWith('diameter'))

它总是匹配并且是空的,但是我给它分配了我想要的键名。

这是最好的方法吗?我是否滥用了pyparsing解析来做一些它不应该做的事情?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-12 23:25:47

请参阅张贴代码中的注释。

代码语言:javascript
复制
import pyparsing as pyp

comma = pyp.Literal(',').suppress()
# use parse actions to do type conversion at parse time, so that results fields
# can immediately be used as ints or floats, without additional int() or float()
# calls
floatnum = pyp.Regex(r'([\d\.]+)').setParseAction(lambda t: float(t[0]))
integer = pyp.Word(pyp.nums).setParseAction(lambda t: int(t[0]))

# define the command keyword - I assume there will be other commands too, they
# should follow this general pattern (define the command keyword, then all the
# options, then define the overall command)
aperture_defn_command_keyword = pyp.Literal('AD')

# define a results name for the matched integer - I don't know what this
# option is, wasn't in your original post
d_option = 'D' + integer.setResultsName('D')

# shortcut for defining a results name is to use the expression as a 
# callable, and pass the results name as the argument (I find this much
# cleaner and keeps the grammar definition from getting messy with lots
# of calls to setResultsName)
circular_aperture_defn = 'C' + comma + floatnum('diameter') + 'X'

# define the overall command
aperture_defn_command = aperture_defn_command_keyword("command") + d_option + pyp.Optional(circular_aperture_defn)

# use searchString to skip over '%'s and '*'s, gives us a ParseResults object
test = "%ADD22C,0.35X*%"
appData = aperture_defn_command.searchString(test)[0]

# ParseResults can be accessed directly just like a dict
print appData['command']
print appData['D']
print appData['diameter']

# or if you prefer attribute-style access to results names
print appData.command
print appData.D
print appData.diameter

# convert ParseResults to an actual Python dict, removes all unnamed tokens
print appData.asDict()

# dump() prints out the parsed tokens as a list, then all named results
print appData.dump()

指纹:

代码语言:javascript
复制
AD
22
0.35
AD
22
0.35
{'diameter': 0.34999999999999998, 'command': 'AD', 'D': 22}
['AD', 'D', 22, 'C', 0.34999999999999998, 'X']
- D: 22
- command: AD
- diameter: 0.35
票数 4
EN

Stack Overflow用户

发布于 2013-10-12 21:21:47

如果您想将您的floatnum命名为“直径”,可以使用命名结果

代码语言:javascript
复制
cmd_app_def_opt_circ = pyp.Group(pyp.Literal('C') +
comma)("circle")


circular_apperture = pyp.Group(cmd_app_def_opt_circ +
pyp.Group(floatnum)("diameter") +
pyp.Literal('X').suppress())

这样,每次解析在floatnum上下文中遇到circular_appertur时,这个结果都被命名为diameter。同样,正如上面所描述的,您可以以相同的方式命名circle。这个对你有用吗?

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19338063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档