length = 80 # longest line allowed def fix_string(s): A = [] i = 0 while i > -1 : next_i = s.find(' ', i + 1) if next_i > length: A.append(s[:i]) s = s[i:] i = 0 else: i = next_i else: A.append(s) return A seg = environment.input_segment_iterator() out = seg.output() while seg.move_one(): if seg.segment_id() != 'NTE': out += seg.output() if seg.segment_id() == 'NTE': for n in range(0, seg.field(3).count_of_repeat()): for s in fix_string(seg.field(3).repeat_field(n).value): out += 'NTE|' + '||' + s + '\r' # reiterate NTE segments to have sequential NTE-1 values seg.reset(out) out = seg.output() j=0 while seg.move_one(): if seg.segment_id() == 'NTE': j+=1 seg.field(1).value =str(j) out += seg.output() value=out