pyparsing

pyparsing question

This code works: from pyparsing import * zipRE = "\d{5}(?:[-\s]\d{4})?" fooRE = "^\!\s+.*" zipcode = Regex( zipRE ) foo = Regex( fooRE ) query = ( zipcode | foo ) tests = [ "80517", "C6H5OH", "90001-3234", "! sfs" ] for t in tests: try: results = query.parseString( t ) print t,"->", results except ParseEx...

Python - pyparsing unicode characters

Hi..:) I tried using w = Word(printables), but it isn't working. How should I give the spec for this. 'w' is meant to process Hindi characters (UTF-8) The code specifies the grammar and parses accordingly. 671.assess :: अहसास ::2 x=number + "." + src + "::" + w + "::" + number + "." + number If there is only english characters it ...

Pyparsing CSV string with random quotes

Hey, I have a string like the following: <118>date=2010-05-09,time=16:41:27,device_id=FE-2KA3F09000049,log_id=0400147717,log_part=00,type=statistics,subtype=n/a,pri=information,session_id=o49CedRc021772,from="[email protected]",mailer="mta",client_name="example.org,[194.177.17.24]",resolved=OK,to="[email protected]"...

Python/PyParsing: Difficulty with setResultsName

I think I'm making a mistake in how I call setResultsName(): from pyparsing import * DEPT_CODE = Regex(r'[A-Z]{2,}').setResultsName("Dept Code") COURSE_NUMBER = Regex(r'[0-9]{4}').setResultsName("Course Number") COURSE_NUMBER.setParseAction(lambda s, l, toks : int(toks[0])) course = DEPT_CODE + COURSE_NUMBER course.setResultsName("c...

PyParsing: What does Combine() do?

What is the difference between: foo = TOKEN1 + TOKEN2 and foo = Combine(TOKEN1 + TOKEN2) Thanks. UPDATE: Based on my experimentation, it seems like Combine() is for terminals, where you're trying to build an expression to match on, whereas plain + is for non-terminals. But I'm not sure. ...

PyParsing: Not all tokens passed to setParseAction()

I'm parsing sentences like "CS 2110 or INFO 3300". I would like to output a format like: [[("CS" 2110)], [("INFO", 3300)]] To do this, I thought I could use setParseAction(). However, the print statements in statementParse() suggest that only the last tokens are actually passed: >>> statement.parseString("CS 2110 or INFO 3300") Match...

PyParsing: Is this correct use of setParseAction()?

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:]): ...

Python: How best to parse a simple grammar?

Ok, so I've asked a bunch of smaller questions about this project, but I still don't have much confidence in the designs I'm coming up with, so I'm going to ask a question on a broader scale. I am parsing pre-requisite descriptions for a course catalog. The descriptions almost always follow a certain form, which makes me think I can par...

pyparsing ambiguity

I'm trying to parse some text using PyParser. The problem is that I have names that can contain white spaces. So my input might look like this. First, a list of names: Joe bob Jimmy X grjiaer-rreaijgr Y Then, things they do: Joe A bob B Jimmy X C the problem of course is that a thing they do can be the same as the end of the name: ...

Strip text except from the contents of a tag

The opposite may be achieved using pyparsing as follows: from pyparsing import Suppress, replaceWith, makeHTMLTags, SkipTo #... removeText = replaceWith("") scriptOpen, scriptClose = makeHTMLTags("script") scriptBody = scriptOpen + SkipTo(scriptClose) + scriptClose scriptBody.setParseAction(removeText) data = (scriptBody).transformStrin...

what next after pyparsing?

I have a huge grammar developed for pyparsing as part of a large, pure python application. I have reached the limit of performance tweaking and I'm at the point where the diminishing returns make me start to look elsewhere. Yes, I think I know most of the tips and tricks and I've profiled my grammar and my application to dust. What next...

"Deparsing" a list using pyparsing

Is it possible to give pyparsing a parsed list and have it return the original string? ...

how do I run a python script that requires pyparsing?

I got a python file which using something called pyparsing but when I run it It showed an error that pyparsing is required can any one pls tel me what to do not that I am a dump in that thing called pything I need to run that script only :)thanks ...

pyparsing matching any combination of specified Literals

Example: I have the literals "alpha", "beta", "gamma". How do I make pyparsing parse the following inputs: alpha alpha|beta beta|alpha|gamma The given input can be constructed by using one or more non-repeating literals from a given set, separated by "|". Advice on setting up pyparsing will be appreciated. ...

Parsing Snort Logs with PyParsing

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.)...

python: replacing regex with BNF or pyparsing

I am parsing a relatively simple text, where each line describes a game unit. I have little knowledge of parsing techniques, so I used the following ad hoc solution: class Unit: # rules is an ordered dictionary of tagged regex that is intended to be applied in the given order # the group named V would correspond to the value (if...

How can fractional number expressions be parsed using pyparsing?

We've just started to kick the tires pyparsing and like it so far, but we've been unable to get it to help us parse fractional number strings to turn them into numeric data types. For example, if a column value in a database table contained the string: 1 1/2 We'd like some way to convert it into the numeric python equivalent: 1.5 We...

python parsing a block of text betwen 2 known lines

Hi, I am trying to get a block of lines between 2 known lines using pyparsing. For example: ABC .... DEF My python code: end = Literal("\n").suppress() firstLine = Literal("ABC") + SkipTo(end) secondLine = Literal("DEF") + SkipTo(end) line = SkipTo(end) test = firstLine + OneOrMore(line) + secondLine test.searchString(myText) -->...