pyparsing

Distinguish matches in pyparsing

六月ゝ 毕业季﹏ 提交于 2019-12-03 03:30:43
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 thing what I found after some research was the items method of the ParseResults class. But it only

PyParsing: Is this correct use of setParseAction()?

雨燕双飞 提交于 2019-12-03 03:23:44
I have strings like this: "MSE 2110, 3030, 4102" I would like to output: [("MSE", 2110), ("MSE", 3030), ("MSE", 4102)] This is my way of going about it, although I haven't quite gotten it yet: def makeCourseList(str, location, tokens): print "before: %s" % tokens for index, course_number in enumerate(tokens[1:]): tokens[index + 1] = (tokens[0][0], course_number) print "after: %s" % tokens course = Group(DEPT_CODE + COURSE_NUMBER) # .setResultsName("Course") course_data = (course + ZeroOrMore(Suppress(',') + COURSE_NUMBER)).setParseAction(makeCourseList) This outputs: >>> course.parseString("CS

Parsing Snort Logs with PyParsing

让人想犯罪 __ 提交于 2019-12-03 03:10:54
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 on the wrong track, but I don't think so (a similar form works perfectly for syslog-type logs, but those

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

这一生的挚爱 提交于 2019-12-02 19:09:10
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 example would be input_string = x > 7 AND x < 8 AND x = 4 parsed_expr = [[['x', '>', 7], 'AND', ['x', '

Adding external information to ParseResults before return

﹥>﹥吖頭↗ 提交于 2019-12-02 09:52:43
I want to add external information to ParseResults before return. I return the results of parsing as asXML(). The external data represented as dictionary so as to parsed as XML in the final parsing. This the code before adding external data from pyparsing import * # a hypothetical outer parser, with an unparsed SkipTo element color = oneOf("red orange yellow green blue purple") expression = SkipTo("XXX") + Literal("XXX").setResultsName('ex') + color.setResultsName('color') data = "JUNK 100 200 10 XXX green" print expression.parseString(data).dump() # main grammar def minorgrammar(toks): # a

pyparsing, Each, results name

风流意气都作罢 提交于 2019-12-02 07:09:15
问题 I'm trying to use pyparsing to build a little not-quite-sql parser (I don't have from clauses, I don't have any joins, etc). I've been basing my work today on the simpleSQL.py example script included with pyparsing. I'm trying to add the "GROUP BY" and "ORDER BY" clauses to the parser, but trying to match them no matter which comes before the other. I'm using the Each class, and it seems to be matching them, but it does not set the result name from within the Each class. I am either not doing

pyparsing - How to parse string with comparison operators?

和自甴很熟 提交于 2019-12-02 06:40:45
问题 So, I have a NumericStringParser class (extracted from here), defined as below: from __future__ import division from pyparsing import Literal, CaselessLiteral, Word, Combine, Group, Optional, ZeroOrMore, Forward, nums, alphas, oneOf, ParseException import math import operator class NumericStringParser(object): def __push_first__(self, strg, loc, toks): self.exprStack.append(toks[0]) def __push_minus__(self, strg, loc, toks): if toks and toks[0] == "-": self.exprStack.append("unary -") def _

Best practice to parse multiple config files

元气小坏坏 提交于 2019-12-02 06:17:56
问题 What would be the best practice - if there is any - to parse multiple config files? I want to parse the mysql server configuration and also write the configuration again. The configuration allows to issue multiple lines like: !includedir /etc/mysql.d/ So the interesting thing is, that some configuration may be located in the main file but other may be located in a sub file. I think pyparsing only works on ONE single file or one content string. So I probably first need to read all files and

pyparsing - How to parse string with comparison operators?

早过忘川 提交于 2019-12-02 04:10:01
So, I have a NumericStringParser class (extracted from here ), defined as below: from __future__ import division from pyparsing import Literal, CaselessLiteral, Word, Combine, Group, Optional, ZeroOrMore, Forward, nums, alphas, oneOf, ParseException import math import operator class NumericStringParser(object): def __push_first__(self, strg, loc, toks): self.exprStack.append(toks[0]) def __push_minus__(self, strg, loc, toks): if toks and toks[0] == "-": self.exprStack.append("unary -") def __init__(self): point = Literal(".") e = CaselessLiteral("E") fnumber = Combine(Word("+-" + nums, nums) +

Best practice to parse multiple config files

落花浮王杯 提交于 2019-12-02 02:43:48
What would be the best practice - if there is any - to parse multiple config files? I want to parse the mysql server configuration and also write the configuration again. The configuration allows to issue multiple lines like: !includedir /etc/mysql.d/ So the interesting thing is, that some configuration may be located in the main file but other may be located in a sub file. I think pyparsing only works on ONE single file or one content string. So I probably first need to read all files and maybe restructures the contents like adding headers for the different files... ====main file==== [mysql]