mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 12:17:08 +00:00
22 lines
487 B
Python
22 lines
487 B
Python
|
|
#!/usr/bin/python3
|
||
|
|
|
||
|
|
def parse_fwr (string, widths, start=0):
|
||
|
|
"""Parse a fixed-width record.
|
||
|
|
|
||
|
|
string: the string to parse.
|
||
|
|
widths: a list of record widths. A negative width denotes a field to be skipped.
|
||
|
|
start: optional start index.
|
||
|
|
|
||
|
|
Returns a list of strings.
|
||
|
|
"""
|
||
|
|
results = []
|
||
|
|
current_index = start
|
||
|
|
for width in widths:
|
||
|
|
if width > 0:
|
||
|
|
results.append(string[current_index : current_index + width])
|
||
|
|
current_index += width
|
||
|
|
else:
|
||
|
|
current_index -= width
|
||
|
|
|
||
|
|
return results
|