I am trying to read a text file containing several fields structured with a given number of characters each. I know that first field takes n1 characters, second field n2 chars, ...
This is what I have so far, for one line:
# Line
line = 'AAABBCCCCDDDDDE'
# Array structure
slice_structure = [3,2,4,5,1]
sliced_array = []
cursor = 0
for n in slice_structure :
sliced_array.append(line[cursor:cursor+n])
cursor += n
print(sliced_array)
The response is the following:
['AAA', 'BB', 'CCCC', 'DDDDD', 'E']
My intention is to create a function with this code and call it for every line of the file. I am sure there must be a better way to do this.
Thanks in advance.