pyparsing

How to retrieve list of values from result object in PyParsing?

不羁岁月 提交于 2019-12-08 02:56:33
问题 I have a simple example where I would like to parse 2 lines of data. In [1] from pyparsing import Word, nums, OneOrMore, Optional, Suppress, alphanums, LineEnd, LineStart Float = Word(nums + '.' + '-') Name = Word(alphanums) Line = OneOrMore(Float)('data') + Suppress(Optional(';')) + Optional('%') + Optional(Name)('name') Lines = OneOrMore(Line + LineEnd()) string = ''' 1 10 0 T20 1 76 0 T76 ''' result = Lines.parseString(string) In [2] result Out[2] (['1', '10', '0', 'T20', '\n', '1', '76',

How to write grammar for an expression when it can have many possible forms

北城以北 提交于 2019-12-07 20:34:30
I have some sentences that I need to convert to regex code and I was trying to use Pyparsing for it. The sentences are basically search rules, telling us what to search for. Examples of sentences - LINE_CONTAINS this is a phrase -this is an example search rule telling that the line you are searching on should have the phrase this is a phrase LINE_STARTSWITH However we - this is an example search rule telling that the line you are searching on should start with the phrase However we The rules can be combined too, like- LINE_CONTAINS phrase one BEFORE {phrase2 AND phrase3} AND LINE_STARTSWITH

Parsing a TCL-like text

女生的网名这么多〃 提交于 2019-12-07 11:36:00
问题 I have a configuration text that looks like this: text=""" key1 value1 key2 { value1 value2 } key3 subkey1 { key1 1 key2 2 key3 { value1 } } BLOBKEY name { dont { # comment parse { me } } } key3 subkey2 { key1 value1 } """ The values are plain strings or quoted strings. The keys are just alphanum strings. I know before hand that key2 and key3.subkey1.key4 will hold sets, so I can treat those paths differently. Likewise, I know that BLOBKEY will contain an "escaped" configuration section. The

pyparsing and line breaks

旧时模样 提交于 2019-12-06 23:53:59
问题 I just started with pyparsing and I have problems with line breaks. My grammar is: from pyparsing import * newline = LineEnd () #Literal ('\n').leaveWhitespace () minus = Literal ('-') plus = Literal ('+') lparen = Literal ('(') rparen = Literal (')') ident = Word (alphas) integer = Word (nums) arith = Forward () parenthized = Group (lparen + arith + rparen) atom = ident | integer | parenthized factor = ZeroOrMore (minus | plus) + atom arith << (ZeroOrMore (factor + (minus | plus) ) + factor)

Why is ordered choice in pyparsing failing for my use case?

匆匆过客 提交于 2019-12-06 17:36:27
>>> g = MatchFirst( Literal("scoobydoo"), Literal("scooby") ) >>> g.parseString( "scooby" ) pyparsing.ParseException: Expected "scoobydoo" (at char 0), (line:1, col:1) Is the ParseException thrown because the scooby has already been consumed in the character stream & thus the parser cannot backtrack ? I'm looking for a detailed implementation explanation for this. At the moment, I consider this a bug because why would the parser short-circuit the matching since it has not search all the choices in production rule. UPDATE : Seems like MatchFirst is not exactly equivalent to | operator. Why ? >>

pyparsing: how to get token location?

随声附和 提交于 2019-12-06 13:51:46
问题 I have a simple pyparsing grammar that matches numbers separated by spaces: from pyparsing import * NUMBER = Word( nums ) STATEMENT = ZeroOrMore( NUMBER ) print( STATEMENT.parseString( "1 2 34" ) ) Given 1 2 34 test string it returns 3 strings that are parsed tokens. But how do I find the location of each token in the original string? I need it for "kind of" syntax highlighting. 回答1: Add this parse action to NUMBER: NUMBER.setParseAction(lambda locn,tokens: (locn,tokens[0])) Parse actions can

Python-Parsing a SQL using pyparsing

我的未来我决定 提交于 2019-12-06 11:57:14
问题 I want to parse a complex SQL which has (inner join,outer join) and get the table names used in the SQL. I am able to get the table names if it is simple select but if the SQL has inner join ,left join like below then the result is giving only the first table. select * from xyz inner join dhf on df = hfj where z > 100 I am using the program similar what is present in the below link by Paul. http://pyparsing.wikispaces.com/file/view/select_parser.py/158651233/select_parser.py Can someone tell

How to retrieve list of values from result object in PyParsing?

我只是一个虾纸丫 提交于 2019-12-06 10:48:59
I have a simple example where I would like to parse 2 lines of data. In [1] from pyparsing import Word, nums, OneOrMore, Optional, Suppress, alphanums, LineEnd, LineStart Float = Word(nums + '.' + '-') Name = Word(alphanums) Line = OneOrMore(Float)('data') + Suppress(Optional(';')) + Optional('%') + Optional(Name)('name') Lines = OneOrMore(Line + LineEnd()) string = ''' 1 10 0 T20 1 76 0 T76 ''' result = Lines.parseString(string) In [2] result Out[2] (['1', '10', '0', 'T20', '\n', '1', '76', '0', 'T76', '\n'], {'data': [(['1', '10', '0'], {}), (['1', '76', '0'], {})], 'name': ['T20', 'T76']})

“Deparsing” a list using pyparsing

ぐ巨炮叔叔 提交于 2019-12-06 09:07:50
问题 Is it possible to give pyparsing a parsed list and have it return the original string? 回答1: Yes, you can if you've instructed the parser not to throw away any input. You do it with the Combine combinator. Let's say your input is: >>> s = 'abc,def, ghi' Here's a parser that grabs the exact text of the list: >>> from pyparsing import * >>> myList = Word(alphas) + ZeroOrMore(',' + Optional(White()) + Word(alphas)) >>> myList.leaveWhitespace() >>> myList.parseString(s) (['abc', ',', 'def', ',', '

List of dictionaries and pyparsing

江枫思渺然 提交于 2019-12-06 08:12:44
问题 I'm using pyparsing to construct dictionaries that get appended to a list. When I do this the dictionaries get wrapped in an extra list and there is also an empty dict appended. I have no clue how to fix this. What I want is [{},{},{}] . I am getting [([{}],{})] Why the code from getDict give me what I want and not getDictParse? #! /usr/bin/env python from pyparsing import Literal, NotAny, Word, printables, Optional, Each, Combine, delimitedList, printables, alphanums, nums, White, OneOrMore,