473,511 Members | 17,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pyparsing batch file

Hi,

me again :-)

I would like to parse a small batch file:

file/read-case kepstop.cas
file/read-data keps1500.dat
solve/monitors/residual/plot no
solve/monitors/residual/print yes
/define/boundary-conditions in velocity-inlet 10 0.1 0.1 no 1
it 500
wd keps1500_500.dat
yes
exit

Right now, I use this little example:

from pyparsing import *

input =
open("/home/fab/HOME/Dissertation/CFD/Fluent/Batch/fluent_batch",
'r')
data = input.read()

#------------------------------------------------------------------------
# Define Grammars
#------------------------------------------------------------------------

integer = Word(nums)
hexnums = Word(alphanums)
end = Literal("\n").suppress()
all = SkipTo(end)
#threadname = dblQuotedString
threadname_read_case = Literal("file/read-case")
threadname_read_data= Literal("file/read-data")
threadname_it = Literal("it")
write_data=Literal("wd")
cas_datei= Word(alphanums)
iteration= Word(nums)
write= Word(alphanums)
file_read_data= "file/read-data " + hexnums.setResultsName("rd")

logEntry = threadname_read_case.setResultsName("threadname")
+ cas_datei.setResultsName("cas_datei")+file_read_da ta
logEntry = file_read_data
logEntryNew = threadname_it.setResultsName("threadname") +
iteration.setResultsName("iteration")
logEntryWD = write_data.setResultsName("threadname") +
write.setResultsName("write")

#------------------------------------------------------------------------

for tokens in logEntryNew.searchString(data):
Â*Â*Â*Â*print
Â*Â*Â*Â*printÂ*"IterationÂ*Command=\tÂ*"+Â*tokens. threadname
Â*Â*Â*Â*printÂ*"NumberÂ*ofÂ*Iterations=\tÂ*"+Â*tok ens.iteration
Â*Â*Â*Â*forÂ*xÂ*inÂ*tokens.condition:
Â*Â*Â*Â*Â*Â*Â*printÂ*x
Â*Â*Â*Â*printÂ*50*"-"

for tokens in logEntryWD.searchString(data):
Â*Â*Â*Â*print
Â*Â*Â*Â*printÂ*"WriteÂ*DataÂ*Command=\tÂ*"+Â*token s.threadname
Â*Â*Â*Â*printÂ*"DataÂ*FileÂ*Name=\tÂ*"+Â*tokens.wr ite
Â*Â*Â*Â*forÂ*xÂ*inÂ*tokens.condition:
Â*Â*Â*Â*Â*Â*Â*printÂ*x
Â*Â*Â*Â*printÂ*50*"-"

for tokens in logEntry.searchString(data):
Â*Â*Â*Â*print
Â*Â*Â*Â*printÂ*"noÂ*idea=\tÂ*"+Â*tokens.threadname
Â*Â*Â*Â*printÂ*"DataÂ*File=\tÂ*"+Â*tokens.rd
Â*Â*Â*Â*print
Â*Â*Â*Â*forÂ*xÂ*inÂ*tokens.condition:
Â*Â*Â*Â*Â*Â*Â*printÂ*x
Â*Â*Â*Â*printÂ*50*"-"
Unfortunately, it does not parse the whole file names with
the underscore and I do not know yet, how I can access the
line with 'define/boundary-conditions'. Every 'argument' of
that command should become a separate python variable!?
Does anyone have an idea, how I can achieve this!?
Regards!
Fabian

Oct 17 '07 #1
2 1685
On Oct 17, 4:47 pm, Fabian Braennstroem <f.braennstr...@gmx.dewrote:
<snip>
Unfortunately, it does not parse the whole file names with
the underscore and I do not know yet, how I can access the
line with 'define/boundary-conditions'. Every 'argument' of
that command should become a separate python variable!?
Does anyone have an idea, how I can achieve this!?
Regards!
Fabian
You are trying to match "keps1500_500.dat" with the expression
"Word(alphanums)". Since the filename contains characters other than
alphas and numbers, you must add the remaining characters ("." and
"_") to the expression. Try changing:

write= Word(alphanums)

to:

write= Word(alphanums+"._")
To help you to parse "/define/boundary-conditions in velocity-inlet 10
0.1 0.1 no 1", we would need to know just what these arguments are,
and what values they can take. I'll take a wild guess, and propose
this:

real = Combine(integer + "." + integer)
defineBoundaryConditions = "/define/boundary-conditions" + \
oneOf("in out inout")("direction") + \
Word(alphanums+"-")("conditionName") + \
integer("magnitude") + \
real("initialX") + \
real("initialY") + \
oneOf("yes no")("optional") + \
integer("normal")

(Note I am using the new notation for setting results names,
introduced in 1.4.7 - simply follow the expression with ("name"),
instead of having to call .setResultsName.)

And here is a slight modification to your printout routine, using the
dump() method of the ParseResults class:

for tokens in defineBoundaryConditions.searchString(data):
print
print "Boundary Conditions = "+ tokens.conditionName
print tokens.dump()
print
print 50*"-"
prints:

Boundary Conditions = velocity-inlet
['/define/boundary-conditions', 'in', 'velocity-inlet', '10', '0.1',
'0.1', 'no', '1']
- conditionName: velocity-inlet
- direction: in
- initialX: 0.1
- initialY: 0.1
- magnitude: 10
- normal: 1
- optional: no

Oct 17 '07 #2
Hi Paul,

Paul McGuire wrote:
On Oct 17, 4:47 pm, Fabian Braennstroem <f.braennstr...@gmx.dewrote:
<snip>
>Unfortunately, it does not parse the whole file names with
the underscore and I do not know yet, how I can access the
line with 'define/boundary-conditions'. Every 'argument' of
that command should become a separate python variable!?
Does anyone have an idea, how I can achieve this!?
Regards!
Fabian

You are trying to match "keps1500_500.dat" with the expression
"Word(alphanums)". Since the filename contains characters other than
alphas and numbers, you must add the remaining characters ("." and
"_") to the expression. Try changing:

write= Word(alphanums)

to:

write= Word(alphanums+"._")
To help you to parse "/define/boundary-conditions in velocity-inlet 10
0.1 0.1 no 1", we would need to know just what these arguments are,
and what values they can take. I'll take a wild guess, and propose
this:

real = Combine(integer + "." + integer)
defineBoundaryConditions = "/define/boundary-conditions" + \
oneOf("in out inout")("direction") + \
Word(alphanums+"-")("conditionName") + \
integer("magnitude") + \
real("initialX") + \
real("initialY") + \
oneOf("yes no")("optional") + \
integer("normal")

(Note I am using the new notation for setting results names,
introduced in 1.4.7 - simply follow the expression with ("name"),
instead of having to call .setResultsName.)

And here is a slight modification to your printout routine, using the
dump() method of the ParseResults class:

for tokens in defineBoundaryConditions.searchString(data):
print
print "Boundary Conditions = "+ tokens.conditionName
print tokens.dump()
print
print 50*"-"
prints:

Boundary Conditions = velocity-inlet
['/define/boundary-conditions', 'in', 'velocity-inlet', '10', '0.1',
'0.1', 'no', '1']
- conditionName: velocity-inlet
- direction: in
- initialX: 0.1
- initialY: 0.1
- magnitude: 10
- normal: 1
- optional: no
Great! Thanks for the very good explanation!

Regards!
Fabian

Oct 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1454
by: Paul McGuire | last post by:
"The best laid plans o' mice an' men / Gang aft a-gley" So said Robert Burns (who really should do something about that speech impediment!). And so said I about 6 weeks ago, when I thought that...
4
2065
by: the.theorist | last post by:
Hey, I'm trying my hand and pyparsing a log file (named l.log): FIRSTLINE PROPERTY1 DATA1 PROPERTY2 DATA2 PROPERTYS LIST ID1 data1 ID2 data2
3
1985
by: rh0dium | last post by:
Hi all, I have a file which I need to parse and I need to be able to break it down by sections. I know it's possible but I can't seem to figure this out. The sections are broken by <> with...
13
2044
by: 7stud | last post by:
To the developer: 1) I went to the pyparsing wiki to download the pyparsing module and try it 2) At the wiki, there was no index entry in the table of contents for Downloads. After searching...
0
2033
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
1
2627
by: Steve | last post by:
Hi All (especially Paul McGuire!) Could you lend a hand in the grammar and paring of the output from the function win32pdhutil.ShowAllProcesses()? This is the code that I have so far (it is...
1
353
by: Neal Becker | last post by:
I'm just trying out pyparsing. I get stack overflow on my first try. Any help? #/usr/bin/python from pyparsing import Word, alphas, QuotedString, OneOrMore, delimitedList first_line = ''...
3
1704
by: hubritic | last post by:
I am trying to parse data that looks like this: IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION 2BFA76F6 1208230607 T S SYSPROC SYSTEM SHUTDOWN BY USER...
5
1470
by: Paul McGuire | last post by:
I've just uploaded to SourceForge and PyPI the latest update to pyparsing, version 1.5.1. It has been a couple of months since 1.5.0 was released, and a number of bug-fixes and enhancements have...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.