I am trying to write a generic function to be able to read a fixed length file. I could go through and hand count the length between columns, and then read the file, but I was wondering if there was a way to do it programmatically.
I can see what needs to be done, but I am not sure the proper way to do it…
If I have a file like:
ColA ColB ColC FinalCol
1 22 23 ColumnsCnBTxt
213 1 2 2
11213 11111 1234567890 3
All of the headers are “right justified” and seperated by spaces (not tabs), so I basically just need to count from StartIndex to last Character and that is my column length.
Is there any easy way to achieve this in python? The resulting object would be a list of column lengths
header_line = " ColA ColB ColC FinalCol"
result = get_header_information(header_line)
#result = (5,5, 10, 13)
One-liner using regex splits:
Explanation:
re.splitsplits a string at all points where a regular expression matches. The regular expression I use (others would be possible) has a lookbehind group(?<=[^ ])which means “preceded by a non-space” and then a space, so matches spaces which are preceded by non-spaces. This will split the string into the column headers, and then we simply take the lengths of the resulting strings.Notice that this is not performance-optimal — we are making three passes through the string and invoking a regex engine — but for normal-size strings that’s fine.