pyparsing

How to match parentheses / brackets in pyparsing

只谈情不闲聊 提交于 2019-12-31 02:00:06
问题 I have a grammar token specified as: list_value = Suppress(oneOf("[ (")) + Group( delimitedList(string_value | int_value))("list") + Suppress(oneOf("] )")) However, this obviously allows (foo, bar] How do I enforce that the lists opening and closing characters must match? 回答1: You make a list a choice between two rules: one for parentheses and one for square brackets. Thanks for bringing up pyparsing. I like it. My answer for your question is: delim_value = Group(delimitedList(string_value |

Skip to first possibility in text with pyparsing

怎甘沉沦 提交于 2019-12-24 11:32:02
问题 I am using pyparsing and am trying to use the method Skipto to reach the first occurence of several possible Literals in the text. Imagine something similar to this: OneOrMore(SkipTo(...longer expression...) | SkipTo(...another long expression...)) And I cannot fuse the two SkipTo's as they are located in different classes and it would not fit into the current system to fuse those classes. If I now have a text similar to this one: ...a lot of stuff... Example2 ...more stuff... Example1 ..

Pyparsing infixNotation into a parse tree: Only one operator ending up in tree?

痞子三分冷 提交于 2019-12-24 07:30:12
问题 My eventual goal is to apply sql-where-clause-style queries to filter pandas dataframes. Some searching led me to pyparsing's infixNotation method. I found an example of infix notation here: http://nullege.com/codes/show/src%40p%40y%40pyparsing-2.0.2%40examples%40simpleBool.py/15/pyparsing.infixNotation/python# but that actually processes the notation, and I need to use the tree multiple times on different data. Looking for help with parse trees I found this: http://pyparsing.wikispaces.com

Matching against a large number of strings containing spaces in pyparsing

两盒软妹~` 提交于 2019-12-24 06:44:11
问题 With pyparsing I need to write a matcher for expressions like a + names + c with a = pp.OneOrMore(pp.Word(pp.alphas)) c = pp.OneOrMore(pp.Word(pp.nums)) and names matching one of many entries in the string list names_list . The two complications are: The entries in names_list can contain spaces . The matching needs to be case-insensitive . names_list is rather large (~20000 entries) I tried names_kw_list = [pp.Keyword(name, caseless=True) for name in names_list ] names = pp.Or(names_kw_list)

How do I implement this in ply, given how pyparsing works

*爱你&永不变心* 提交于 2019-12-24 05:51:09
问题 I'm trying to implement something in ply, which I'm very new to, based on what I have done in pyparsing, which I'm also quite new to. How can I write a simple nesting search such as this: thecontent = pyparsing.Word(pyparsing.alphanums) | '&' | '|' parens = pyparsing.nestedExpr( '(', ')', content=thecontent) By using PLY? 回答1: What you have written in pyparsing does not translate well to PLY because, well, it's barely even a parser. nestedExpr is a helper for quickly defining an expression of

pyparsing: setResultsName for multiple elements get combined

那年仲夏 提交于 2019-12-24 01:17:24
问题 Here is the text I'm parsing: x ~ normal(mu, 1) y ~ normal(mu2, 1) The parser matches those lines using: model_definition = Group(identifier.setResultsName('random_variable_name') + '~' + expression).setResultsName('model_definition') // end of line: .setResultsName('model_definition') The problem is that when there are two model definitions, they aren't named separately in the ParseResults object: It looks like the first one gets overridden by the second. The reason I'm naming them is to

Capture literal brackets inside brackets in pyparsing

一曲冷凌霜 提交于 2019-12-23 22:00:47
问题 I'm trying to parse some functions arguments with PyParsing but am having trouble getting the right syntax. Namely, given: str = "(key=val())" I would like the parser to return ['key', 'val()'] . I've been trying to get this to work with the following code; the .suppress() calls are intentionally omitted for clarity. ob = Literal("(") cb = Literal(")") key = Word(alphas) value = Word(alpha + "()") parser = ob + key + "=" + value + cb print parser.parseString(str) but of course it's matching

PyParsing: Is it possible to globally suppress all Literals?

心已入冬 提交于 2019-12-23 18:43:09
问题 I have a simple data set to parse with lines like the following: R1 (a/30) to R2 (b/30), metric 30 The only data that I need from the above is as follows: R1, a, 30, R2, 192.168.0.2, 30, 30 I can parse all of this easily with pyparsing, but I either end up with a bunch of literals in my output, or I have to specifically say Literal(thing).suppress() in my parsing grammar, which gets tiresome. Ideally, I'd like to write a grammar for the above like: Word(alphanums) + '(' + Word(alphanums) + '/

Raise a custom exception in pyparsing

匆匆过客 提交于 2019-12-23 17:22:46
问题 I have defined these as part of my grammar in pyparsing argument = oneOf(valid_arguments) function_name = oneOf(valid_functions) function_call = Group(function_name + LPAR + argument + RPAR) where valid_argument and valid_function are lists of strings containing valid argument names and function names respectively. Now I want to generate custom exceptions in case if the column name is not part of valid_arguments and function_name is not one of the function names. How can I do that with

PyParsing non-greedy match

穿精又带淫゛_ 提交于 2019-12-23 09:59:12
问题 I am trying to parse a partially standardized street address into it's components using pyparsing . I want to non-greedy match a street name that may be N tokens long. For example: 444 PARK GARDEN LN Should be parsed into: number: 444 street: PARK GARDEN suffix: LN How would I do this with PyParsing? Here's my initial code: from pyparsing import * def main(): street_number = Word(nums).setResultsName('street_number') street_suffix = oneOf("ST RD DR LN AVE WAY").setResultsName('street_suffix')