Python-list Digest, Vol 32, Issue 300
Khoa Nguyen
khoa.coffee at gmail.com
Wed May 17 15:48:58 EDT 2006
> Optional(f1SpecificFormat) + "," + Optional(f2SpecificFormat) + "," + ...
> and so on.
Thanks for your input again. This is exactly what I am trying. The
only thing is it can get ugly if the number of fields are many -- I
have about 61 fields right now. Just wondering if there is a
"cleaner" approach.
Thanks,
Khoa
==================
from pyparsing import *
DELIM = Suppress('&')
RECORD_END = Suppress(';')
f1 = Literal('a')
f2 = Literal('b')
f3 = Word(alphanums+'_-.')
common = f1
extended = Optional(DELIM + Optional(f2)) \
+ Optional(DELIM + Optional(f3))
g = common + Optional(extended) + RECORD_END
def parse(record):
result = g.parseString(record)
print result
if __name__=='__main__':
good = ['a;', 'a&&c-123;', 'a&b;', 'a&b&c;', 'a&b&c-123-af;']
bad = [ ';', # required field missing
'a', # RECORD_END missing
'b&c;', # required field missing
'a&c&b;', # Incorrect order
'x123&a&b;' # Incorrect order
]
for r in good + bad:
try:
parse(r)
except ParseException, err:
print err.line
print " "*(err.column-1) + "^"
print err
continue
=======================
More information about the Python-list
mailing list