Search code examples
pythonparsingcoda-format

Proper way to read position based text file


So I have a file with data in this (standardized) format:

 12455WE READ THIS             TOO796445 125997  554777     
 22455 888AND THIS       TOO796445 125997  55477778 2 1

Probably tought up by someone who has done too much cobol.

Each field has a fixed lenght and I can read it by slicing the line.

My problem is how can I structure my code in a way that makes it more flexible and does not make me use hard-coded offsets for the slices ? Should I use a class of constants of something like that ?

EDIT:

Also the first number (0->9 always present) determines the structure of the line which is of fixed length. Also the file is provided by a 3rd party who ensures the validity so I don't need to check the format only read it. There are around 11 different line structures.


Solution

  • Create a list of widths and a routine that accepts this and an indexed column number as parameters. The routine can calculate the start offset for your slice by adding all previous column widths, and add the width of the indexed column for the end offset.