Fixed-length fields in Martel

Andrew Dalke adalke at mindspring.com
Mon Jun 2 12:14:57 EDT 2003


Michael Schmit:
> Is the a way to handle fixed-length fields with Martel. The often used
> 'split-by-whitespace'-approach fails, if fields can be empty.

Do you mean my Martel, the regexp parser which generates SAX events?

You could do

field = Group("fixed_field", Re(".{10}"))
fields = Rep(field)

That would parse your whole file in groups of 10.

If you had something more complicated, like

6 characters for the field name
  - if the field name is "ATOM  " then
      1 space
      4 characters for the atom name
      6 characters * 3 for the coordinate

you could do

def fixed_width(name, size):
  return Group(name, Re("."*size))

ATOM_LINE = (Str("ATOM  ") + Str(" ") + fixed_width("name", 4) +
      fixed_width("x", 6) + fixed_width("y", 6) + fixed_width("z", 6) +
AnyEol())

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list