473,671 Members | 2,279 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.da t
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").s uppress()
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=Lite ral("wd")
cas_datei= Word(alphanums)
iteration= Word(nums)
write= Word(alphanums)
file_read_data= "file/read-data " + hexnums.setResu ltsName("rd")

logEntry = threadname_read _case.setResult sName("threadna me")
+ cas_datei.setRe sultsName("cas_ datei")+file_re ad_data
logEntry = file_read_data
logEntryNew = threadname_it.s etResultsName(" threadname") +
iteration.setRe sultsName("iter ation")
logEntryWD = write_data.setR esultsName("thr eadname") +
write.setResult sName("write")

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

for tokens in logEntryNew.sea rchString(data) :
Â*Â*Â*Â*print
Â*Â*Â*Â*printÂ* "IterationÂ*Com mand=\tÂ*"+Â*to kens.threadname
Â*Â*Â*Â*printÂ* "NumberÂ*ofÂ*It erations=\tÂ*"+ Â*tokens.iterat ion
Â*Â*Â*Â*forÂ*x *inÂ*tokens.con dition:
Â*Â*Â*Â*Â*Â*Â*p rintÂ*x
Â*Â*Â*Â*printÂ* 50*"-"

for tokens in logEntryWD.sear chString(data):
Â*Â*Â*Â*print
Â*Â*Â*Â*printÂ* "WriteÂ*DataÂ*C ommand=\tÂ*"+Â* tokens.threadna me
Â*Â*Â*Â*printÂ* "DataÂ*FileÂ*Na me=\tÂ*"+Â*toke ns.write
Â*Â*Â*Â*forÂ*x *inÂ*tokens.con dition:
Â*Â*Â*Â*Â*Â*Â*p rintÂ*x
Â*Â*Â*Â*printÂ* 50*"-"

for tokens in logEntry.search String(data):
Â*Â*Â*Â*print
Â*Â*Â*Â*printÂ* "noÂ*idea=\tÂ*" +Â*tokens.threa dname
Â*Â*Â*Â*printÂ* "DataÂ*File=\t *"+Â*tokens. rd
Â*Â*Â*Â*print
Â*Â*Â*Â*forÂ*x *inÂ*tokens.con dition:
Â*Â*Â*Â*Â*Â*Â*p rintÂ*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 1695
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.d at" 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)
defineBoundaryC onditions = "/define/boundary-conditions" + \
oneOf("in out inout")("direct ion") + \
Word(alphanums+ "-")("conditionNa me") + \
integer("magnit ude") + \
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 defineBoundaryC onditions.searc hString(data):
print
print "Boundary Conditions = "+ tokens.conditio nName
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>
>Unfortunatel y, 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.d at" 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)
defineBoundaryC onditions = "/define/boundary-conditions" + \
oneOf("in out inout")("direct ion") + \
Word(alphanums+ "-")("conditionNa me") + \
integer("magnit ude") + \
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 defineBoundaryC onditions.searc hString(data):
print
print "Boundary Conditions = "+ tokens.conditio nName
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
1462
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 I would leave pyparsing alone for awhile, after the 1.3.1 release. Well, here we are, and I'm announcing the 1.3.2 release, with some changes that I'd like to get released quickly. Here is the excerpt from the change log:
4
2071
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
1994
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 one or more keywords in the <>. What I want to do is to be able to pars a particular section of the file. So for example I need to be able to look at the SYSLIB section. Presumably the sections are
13
2055
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 around a bit, I finally discovered a tiny link buried in some text at the top of the home page. 3) Link goes to sourceforge. At sourceforge, there was a nice, green 'download' button that stood out from the page. 4) I clicked on the download...
0
2040
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 word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file.
1
2641
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 very clumsy at the moment) : import string
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 = '' def main():
3
1714
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 A6D1BD62 1215230807 I H Firmware Event My problem is that sometimes there is a RESOURCE_NAME and sometimes not, so I wind up with "Firmware" as my RESOURCE_NAME and "Event" as
5
1486
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 accumulated in SVN, so time for a release! Here's what's new in Pyparsing 1.5.1: - Added __dir__() methods to ParseBaseException and ParseResults, to support new dir() behavior in Py2.6 and Py3.0. If dir() is called on a ParseResults object,...
0
8481
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8602
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7441
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1814
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.