473,287 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 1680
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
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
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
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
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
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
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
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
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
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.