473,385 Members | 1,325 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,385 software developers and data experts.

how to parse structured text file?

I have a file which contains data in the format shown in the sample
bellow.
How can I parse it to get following:
(14,trigger,guard,do_action,15)

Thanks a lot for your postings
Petr Jakes

type:
4
bgrColor:
255 255 255
fgrColor:
0 0 0
objId:
16
Num.Pts:
2
177 104
350 134
objStartId:
14
objEndId:
15
eventName:
trigger
eventCond:
guard
eventAction:
do_action

Feb 1 '06 #1
1 3204
A problem fit for pyparsing! Download pyparsing at
http://pyparsing.sourceforge.net.

Assuming you always have these fields, in this order, this program will
figure them out. If not, you'll need to tweak the pyparsing
definitions as needed.

-- Paul
data = """type:
4
bgrColor:
255 255 255
fgrColor:
0 0 0
objId:
16
Num.Pts:
2
177 104
350 134
objStartId:
14
objEndId:
15
eventName:
trigger
eventCond:
guard
eventAction:
do_action
"""

from pyparsing import *

# define literals for field labels
type_ = Literal("type")
bgrColor = Literal("bgrColor")
fgrColor = Literal("fgrColor")
objId = Literal("objId")
numPts = Literal("Num.Pts")
objStartId = Literal("objStartId")
objEndId = Literal("objEndId")
eventName = Literal("eventName")
eventCond = Literal("eventCond")
eventAction = Literal("eventAction")

# define an integer, and tell parser to convert them to ints
intvalue = Word(nums).setParseAction( lambda s,l,toks: int(toks[0]) )

# define an alphabetic identifier
alphavalue = Word(alphas,alphanums+"_")

# define a 2D coordinate, with results names for fields
coordvalue = Group( intvalue.setResultsName("X") +
intvalue.setResultsName("Y") )

# define an RGB color value, with results names for fields
colorvalue = Group( intvalue.setResultsName("R") +
intvalue.setResultsName("G") +
intvalue.setResultsName("B") )

# compose an entry definition, using above-defined expressions, with
results names for fields
entry = ( type_ + ":" + "4" +
bgrColor + ":" + colorvalue.setResultsName("bgrColor") +
fgrColor + ":" + colorvalue.setResultsName("fgrColor") +
objId + ":" + intvalue.setResultsName("objId") +
numPts + ":" + Group( intvalue.setResultsName("numpts") +
OneOrMore( coordvalue ).setResultsName("coords")
).setResultsName("pts") +
objStartId + ":" + intvalue.setResultsName("objStartId") +
objEndId + ":" + intvalue.setResultsName("objEndId") +
eventName + ":" + alphavalue.setResultsName("eventName") +
eventCond + ":" + alphavalue.setResultsName("eventCond") +
eventAction + ":" + alphavalue.setResultsName("eventAction")
)

# scan through input data, and retrieve data fields as desired
for entryData,start,end in entry.scanString(data):
print
"(%(objStartId)d,%(eventName)s,%(eventCond)s,%(eve ntAction)s,%(objEndId)d)"
% entryData
print entryData.objId
print entryData.bgrColor
print entryData.fgrColor
print [ (pt.X,pt.Y) for pt in entryData.pts.coords ]
print [ tuple(pt) for pt in entryData.pts.coords ]
Prints:
(14,trigger,guard,do_action,15)
16
[255, 255, 255]
[0, 0, 0]
[(177, 104), (350, 134)]
[(177, 104), (350, 134)]

Feb 1 '06 #2

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

Similar topics

23
by: Charles Law | last post by:
Does anyone have a regex pattern to parse HTML from a stream? I have a well structured file, where each line is of the form <sometag someattribute='attr'>text</sometag> for example <SPAN...
6
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hello, I have an XML file generated from a third party application that I would like to parse. Ideally, I plan on having a windows service setup to scan various folders for XML files and parse the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.