pyparsing

pyparsing: example JSON parser fails for list of dicts

核能气质少年 提交于 2019-12-13 14:14:59
问题 All, I'm trying to understand how to handle a list of Dicts using pyparsing. I've gone back to the example JSON parser for best practices but I've found that it can't handle a list of dicts either! Consider the following (this is the stock example JSON parser, but with some comments removed and my test case instead of the default one): #!/usr/bin/env python2.7 from pyparsing import * TRUE = Keyword("true").setParseAction( replaceWith(True) ) FALSE = Keyword("false").setParseAction(

`pyparsing`: iterating over `ParsedResults`

白昼怎懂夜的黑 提交于 2019-12-13 13:12:19
问题 I've just started using pyparsing this evening and I've built a complex grammar which describes some sources I'm working with very effectively. It was very easy and very powerful. However, I'm having some trouble working with ParsedResults . I need to be able to iterate over nested tokens in the order that they're found, and I'm finding it a little frustrating. I've abstracted my problem to a simple case: import pyparsing as pp word = pp.Word(pp.alphas + ',.')('word*') direct_speech = pp

Writing grammar rules for context sensitive elements using Pyparsing

故事扮演 提交于 2019-12-13 12:12:27
问题 I am trying to write a grammar for a set of sentences and using Pyparsing to parse it. These sentences tell what and how to search in a text file, and I need to convert them into corresponding regex search codes. However, there are some elements that are not really context-free and hence, I am finding it difficult to write production rules for them. Basically, my aim is to parse these sentences and then write regexes for them. Some examples of context-sensitive elements found in these

Add function parsing to simple pyparsing arithmetics grammar

风格不统一 提交于 2019-12-13 00:56:30
问题 I have this code: import pyparsing as pp point = pp.Literal(".") number = pp.Combine(pp.Word(pp.nums) + pp.Optional(point + pp.Word(pp.nums))) lpar = pp.Literal("(").suppress() rpar = pp.Literal(")").suppress() plus = pp.Literal("+") minus = pp.Literal("-") mult = pp.Literal("*") div = pp.Literal("/") exp = pp.Literal("^") mod = pp.Literal("%") idiv = pp.Literal("//") comma = pp.Literal(',') ident = pp.Combine(pp.Word(pp.alphanums) + pp.Literal("_") + pp.Word(pp.nums)) expr = pp.Forward()

Pyparsing OR operation use shortest string when more than two match

徘徊边缘 提交于 2019-12-12 23:40:57
问题 I need to parse some statements but want the flexibility of using multiple words to signal the of the statement. eg. string = """ start some statement end other stuff in between start some other statement. other stuff in between start another statement """ in this case end , . and end of line are the tokens that will signal the end of the statement I am looking for. I tried the following: from pyparsing import restOfLine, SkipTo skip_to_end_of_line = restOfLine skip_to_dot = SkipTo('.',

Parse and group multiple items together using Pyparse

本秂侑毒 提交于 2019-12-12 18:58:16
问题 This is a build up on Build a simple parser that is able to parse different date formats using PyParse I have a parser that should group one or more users together into a list So a.parser('show abc, xyz commits from "Jan 10,2015" to "27/1/2015"') should group the two usernames into a list [abc,xyz] For users I have: keywords = ["select", "show", "team", "from", "to", "commits", "and", "or"] [select, show, team, _from, _to, commits, _and, _or] = [ CaselessKeyword(word) for word in keywords ]

Python/Pyparsing - Multiline quotes

ⅰ亾dé卋堺 提交于 2019-12-12 17:42:13
问题 I'm trying to use pyparsing to match a multiline string that can continue in a similar fashion to those of python: Test = "This is a long " \ "string" I can't find a way to make pyparsing recognize this. Here is what I've tried so far: import pyparsing as pp src1 = ''' Test("This is a long string") ''' src2 = ''' Test("This is a long " \ "string") ''' _lp = pp.Suppress('(') _rp = pp.Suppress(')') _str = pp.QuotedString('"', multiline=True, unquoteResults=False) func = pp.Word(pp.alphas)

pyparsing a field that may or may not contain values

雨燕双飞 提交于 2019-12-12 12:54:15
问题 I have a dataset that resemebles the following: Capture MICR - Serial: Pos44: Trrt: 32904 Acct: Tc: 2064 Opt4: Split: The problem that I am having is I can't figure out how I could properly write a capture for the "Capture MICR - Serial Field". This field could either be blank or contain an alphanumeric of varying length (I have the same problem with the other fields that could either be populated or blank. I have tried some variations of the following, but am still coming up short. pp

Difficulty of this particular job using pyparsing? (beginner)

别来无恙 提交于 2019-12-12 11:51:04
问题 I have a task to do that I'm sure Python and pyparsing could really help with, but I'm still too much of a novice with programming to make a smart choice about how challenging the complete implementation will be and whether it's worth trying or is certain to be a fruitless time-sink. The task is to translate strings of arbitrary length and nesting depth with a structure following the general grammar of this one: item12345 'topic(subtopic(sub-subtopic), subtopic2), topic2' into an item in a

Parse line data until keyword with pyparsing

落花浮王杯 提交于 2019-12-12 08:57:03
问题 I'm trying to parse line data and then group them in list. Here is my script: from pyparsing import * data = """START line 2 line 3 line 4 END START line a line b line c END """ EOL = LineEnd().suppress() start = Keyword('START').suppress() + EOL end = Keyword('END').suppress() + EOL line = SkipTo(LineEnd()) + EOL lines = start + OneOrMore(start | end | Group(line)) start.setDebug() end.setDebug() line.setDebug() result = lines.parseString(data) results_list = result.asList() print(results