pyparsing

Matching nonempty lines with pyparsing

荒凉一梦 提交于 2019-12-04 03:51:23
问题 I am trying to make a small application which uses pyparsing to extract data from files produced by another program. These files have following format. SOME_KEYWORD: line 1 line 2 line 3 line 4 ANOTHER_KEYWORD: line a line b line c How can i construct grammar which will help to extract line 1 , line 2 ... line 4 and line a .. line c ? I am trying to make a construction like this Grammar = Keyword("SOME_KEYWORD:").supress() + NonEmptyLines + EmptyLine.supress() +\ Keyword("ANOTHER_KEYWORD:")

PyParsing lookaheads and greedy expressions

若如初见. 提交于 2019-12-04 01:25:33
I'm writing a parser for a query language using PyParsing, and I've gotten stuck on (what I believe to be) an issue with lookaheads. One clause type in the query is intended to split strings into 3 parts (fieldname,operator, value) such that fieldname is one word, operator is one or more words, and value is a word, a quoted string, or a parenthesized list of these. My data look like author is william author is 'william shakespeare' author is not shakespeare author is in (william,'the bard',shakespeare) And my current parser for this clause is written as: fieldname = Word(alphas) operator =

Pyparsing - where order of tokens in unpredictable

此生再无相见时 提交于 2019-12-04 01:19:06
问题 I want to be able to pull out the type and count of letters from a piece of text where the letters could be in any order. There is some other parsing going on which I have working, but this bit has me stumped! input -> result "abc" -> [['a',1], ['b',1],['c',1]] "bbbc" -> [['b',3],['c',1]] "cccaa" -> [['a',2],['c',3]] I could use search or scan and repeat for each possible letter, but is there a clean way of doing it? This is as far as I got: from pyparsing import * def handleStuff(string,

Access parsed elements using Pyparsing

隐身守侯 提交于 2019-12-03 21:08:38
I have a bunch of sentences which I need to parse and convert to corresponding regex search code. Examples of my sentences - LINE_CONTAINS phrase one BEFORE {phrase2 AND phrase3} AND LINE_STARTSWITH Therefore we -This means in the line, phrase one comes somewhere before phrase2 and phrase3 . Also, the line must start with Therefore we LINE_CONTAINS abc {upto 4 words} xyz {upto 3 words} pqr -This means I need to allow upto 4 words between the first 2 phrases and upto 3 words between last 2 phrases Using help from Paul Mcguire ( here ), the following grammar was written - from pyparsing import

context in pyparsing parse actions besides globals

浪子不回头ぞ 提交于 2019-12-03 16:43:38
I'd like to be able to parse two (or any number) of expressions, each with their own set of variable definitions or other context. There doesn't seem to be an obvious way to associate a context with a particular invocation of pyparsing.ParseExpression.parseString() . The most natural way seems to be to use an instancemethod of some class as the parse actions. The problem with this approach is that the grammar must be redefined for each parse context (for instance, in the class's __init__ ), which seems terribly inefficient. Using pyparsing.ParseExpression.copy() on the rules doesn't help; the

Does Pyparsing Support Context-Sensitive Grammars?

邮差的信 提交于 2019-12-03 15:44:17
Forgive me if I have the incorrect terminology; perhaps just getting the "right" words to describe what I want is enough for me to find the answer on my own. I am working on a parser for ODL (Object Description Language), an arcane language that as far as I can tell is now used only by NASA PDS (Planetary Data Systems; it's how NASA makes its data available to the public). Fortunately, PDS is finally moving to XML, but I still have to write software for a mission that fell just before the cutoff. ODL defines objects in something like the following manner: OBJECT = TABLE ROWS = 128 ROW_BYTES =

Distinguish matches in pyparsing

淺唱寂寞╮ 提交于 2019-12-03 14:37:48
问题 I want to parse some words and some numbers with pyparsing. Simple right. from pyparsing import * A = Word(nums).setResultsName('A') B = Word(alphas).setResultsName('B') expr = OneOrMore(A | B) result = expr.parseString("123 abc 456 7 d") print result The code above prints ['123', 'abc', '456', '7', 'd'] . So everything worked. Now I want to do some work with these parsed values. For this task, I need to know if they matched A or B . Is there a way to distinguish between these two. The only

Parsing Snort Logs with PyParsing

不问归期 提交于 2019-12-03 12:55:11
问题 Having a problem with parsing Snort logs using the pyparsing module. The problem is with separating the Snort log (which has multiline entries, separated by a blank line) and getting pyparsing to parse each entry as a whole chunk, rather than read in line by line and expecting the grammar to work with each line (obviously, it does not.) I have tried converting each chunk to a temporary string, stripping out the newlines inside each chunk, but it refuses to process correctly. I may be wholly

pyparsing example

随声附和 提交于 2019-12-03 06:59:23
It is my first attempt to use pyparsing and I'd like to ask how to filter this sample line: survey = '''GPS,PN1,LA52.125133215643,LN21.031048525561,EL116.898812''' to get output like: 1,52.125133215643,21.031048525561,116.898812 In general I have problem with understanding pyparsing logic so any help with this example will be appreciated. Thanks You could start with something like this: from pyparsing import * survey = '''GPS,PN1,LA52.125133215643,LN21.031048525561,EL116.898812''' number = Word(nums+'.').setParseAction(lambda t: float(t[0])) separator = Suppress(',') latitude = Suppress('LA')

parsing a complex logical expression in pyparsing in a binary tree fashion

混江龙づ霸主 提交于 2019-12-03 06:38:35
问题 I am trying to parse complex logical expression like the one below; x > 7 AND x < 8 OR x = 4 and get the parsed string as a binary tree. For the above expression the expected parsed expression should look like [['x', '>', 7], 'AND', [['x', '<', 8], 'OR', ['x', '=', 4]]] 'OR' logical operator has higher precedence than 'AND' operator. Parenthesis can override the default precedence. To be more general, the parsed expression should look like; <left_expr> <logical_operator> <right_expr> Another