This topic contains 0 replies, has 1 voice, and was last updated by  lev 3 years, 8 months ago.

How to split long report into fields of limited length using Chameleon

  • Quite often we have long report text in a field and have to split it to lines shorter than field length limit. I posted article how to do it with Translator in another post. This post is about how to do it with Chameleon Python script, in Scripted Transformation script.

    In this example we have to have no more than 80 characters in each NTE-3. Not exceeding maximum line length we will wrap at last complete word and add to HL7 message another NTE segment, with excessive text placed in new NTE-3.

    Please note that tilde character is handled as repetition delimiter. If you prefer to preserve tilde character as element of inline text, then ensure to properly escape it, as ‘\R\’. See example in test data below.

    Here is sample Scripted Transformation script:

    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:
            s = s[i:]
            i = 0
            i = next_i
       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
    out = seg.output()
    while seg.move_one():
       if seg.segment_id() == 'NTE':
          seg.field(1).value =str(j)
       out += seg.output()

    Here is test data.

    NTE|1||Lorem ipsum dolor sit amet,\R\ consectetur adipisicing elit, ~sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    Here are links to vmd file and test data file.

You must be logged in to reply to this topic.