- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我认为我在调用 setResultsName()
时犯了一个错误:
from pyparsing import *
DEPT_CODE = Regex(r'[A-Z]{2,}').setResultsName("Dept Code")
COURSE_NUMBER = Regex(r'[0-9]{4}').setResultsName("Course Number")
COURSE_NUMBER.setParseAction(lambda s, l, toks : int(toks[0]))
course = DEPT_CODE + COURSE_NUMBER
course.setResultsName("course")
statement = course
来自空闲:
>>> myparser import *
>>> statement.parseString("CS 2110")
(['CS', 2110], {'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})
我希望的输出:
>>> myparser import *
>>> statement.parseString("CS 2110")
(['CS', 2110], {'Course': ['CS', 2110], 'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})
setResultsName()
是否只对终端有效?
最佳答案
如果将course
的定义改为
course = (DEPT_CODE + COURSE_NUMBER).setResultsName("Course")
你得到以下行为:
x=statement.parseString("CS 2110")
print(repr(x))
# (['CS', 2110], {'Course': [((['CS', 2110], {'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]}), 0)], 'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})
print(x['Dept Code'])
# CS
print(x['Course Number'])
# 2110
print(x['Course'])
# ['CS', 2110]
这不是您想要的 repr
,但这是否足够?
请注意,from the docs :
[setResultsName] returns a copy of the original ParserElement object; this is so that the client can define a basic element, such as an integer, and reference it in multiple places with different names.
因此 course.setResultsName("Course")
不起作用,因为它不影响 course
。相反,您必须说 course=course.setResultsName("Course")
。这是执行上述操作的另一种方法。
关于Python/PyParsing : Difficulty with setResultsName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2940166/
PEG论文描述了两种语义谓词解析表达式: 和谓词&e 不是谓词!e pyparsing 是否支持 And 谓词?或者这只是排序解析表达式的同义词?在这种情况下,它应该等同于 And 类。对吗? Not
我有以下语法和测试用例: from pyparsing import Word, nums, Forward, Suppress, OneOrMore, Group #A grammar for a
我想用这些条件解析带有嵌套括号的字符串: 元素由逗号分隔 ,或吧 | . 嵌套括号元素可能是单个字母数字或另一个嵌套括号。 每个嵌套括号元素由条 | 连接字面量导致创建一个新序列,该序列将先前的序列元
有以下我要解析的字符串: ((K00134,K00150) K00927,K11389) (K00234,K00235) 每个步骤由空格分隔,交替由逗号表示。我被困在字符串的第一部分,括号内有一个空格
所以这是取自 fourFn.py 的解析器: from pyparsing import ( Literal, Word, Group, Forward, al
所以我在制作解析器时,发现了一个问题。事实上,为了解析数字,我有: from pyparsing import Word, nums n = Word(nums) 这适用于没有千位分隔符的数字。例如,
我有一个简单的数据集,可以使用如下行进行解析: R1 (a/30) to R2 (b/30), metric 30 我需要从上面得到的唯一数据如下: R1, a, 30, R2, 192.168.0.
我正在尝试使用 pyparsing 来构建一个解析器,该解析器将匹配任意嵌套的一组括号内的所有文本。如果我们考虑这样的字符串: "[A,[B,C],[D,E,F],G] Random Middle t
from pyparsing import * class AParseActionHolder(object): def __call__(self, string, index, t):
我正在寻找一种改进使用pyparsing构建的解析器性能的方法。我阅读了关于packrat的解析,看来这确实可以帮助解析器提高性能。但是,当我启用packrat解析时,性能会变差!如果没有packra
我正在尝试使用一些特殊规则(例如相邻值和邻近值)创建 bool 查询解析器。到目前为止我创建的规则是 ## DEFINITIONS OF SYMBOLS ### NEAR = CaselessLite
我编写此脚本是为了使用类似于序言的语法来解析语句,将连接词视为具有优先级的运算符: import pyparsing as pyp alphabet = "abcdefghijklmnopqrstuv
我有一个来自游戏的文件,我正在尝试解析它。以下是摘录: id: 50 #Survival Stage bound: 1500 # phase 0 bandi
我很难理解允许在参数名称中使用“\”的语法(例如 net\)。然而,“\”也可用作续行(参见例 2)。 Ex1 工作正常,但 linebreak 和 identifier 变量之间存在冲突。 Ex1:
我正在尝试学习 pyparsing。这听起来很有希望,并且用于文本处理会很有趣。无论如何,这是我的问题: 我有一个类(class)名称列表。例如, courselist = ["Project Bas
我想利用 cStyleComment 变量,但我想专门处理它们,而不是仅仅忽略这些注释。有什么方法可以让 pyparsing 在将其识别为注释的输入片段上调用我的处理程序,然后再将其丢弃吗? 我正在处
我今天早些时候发布了一个关于同一问题的问题,但由于该问题的解决方案是切换库,而我现在对另一个库有问题,我想我会提出另一个问题......希望没关系。 所以我不想匹配以下标记: ... 我不
我有一个如下所示的数据: data = 'person(firstame="bob", lastname="stewart", dob="2010-0206", hobbies=["reading,
我正在通过解析文件 output=wilcard.parseFile(myfile) print output 我确实只得到了字符串的第一个匹配。 我有一个大的配置文件需要解析,其中的“条目”用大括号
我需要解析以下三行: Uptime is 1w2d Last reset at 23:05:56 Reason: reload 但最后两行并不总是存在,在第一次重新启动之前输出可能如下所示: Up
我是一名优秀的程序员,十分优秀!