Python - reading data from file with variable attributes and line lengths
Question: I'm trying to find the best way to parse through a file in Python and create a
list of namedtuples, with each tuple representing a single data entity and its
attributes. The data looks something like this:

    
    
    UI: T020  
    STY: Acquired Abnormality  
    ABR: acab   
    STN: A1.2.2.2  
    DEF: An abnormal structure, or one that is abnormal in size or location, found   
    in or deriving from a previously normal structure.  Acquired abnormalities are  
    distinguished from diseases even though they may result in pathological   
    functioning (e.g., "hernias incarcerate").   
    HL: {isa} Anatomical Abnormality
    
    UI: T145   
    RL: exhibits   
    ABR: EX   
    RIN: exhibited_by   
    RTN: R3.3.2   
    DEF: Shows or demonstrates.   
    HL: {isa} performs   
    STL: [Animal|Behavior]; [Group|Behavior]   
    
    UI: etc...
    

While several attributes are shared (eg UI), some are not (eg STY). However, I
could hardcode an exhaustive list of necessary.  
Since each grouping is separated by an empty line, I used split so I can
process each chunk of data individually:

    
    
    input = file.read().split("\n\n")
    for chunk in input:
         process(chunk)
    

I've seen some approaches use string find/splice, itertools.groupby, and even
regexes. I was thinking of doing a regex of '[A-Z]*:' to find where the
headers are, but I'm not sure how to approach pulling out multiple lines
afterwards until another header is reached (such as the multilined data
following DEF in the first example entity).

I appreciate any suggestions.


Answer: 
    source = """
    UI: T020  
    STY: Acquired Abnormality  
    ABR: acab   
    STN: A1.2.2.2  
    DEF: An abnormal structure, or one that is abnormal in size or location, found   
    in or deriving from a previously normal structure.  Acquired abnormalities are  
    distinguished from diseases even though they may result in pathological   
    functioning (e.g., "hernias incarcerate").   
    HL: {isa} Anatomical Abnormality
    """
    
    inpt = source.split("\n")  #just emulating file
    
    import re
    reg = re.compile(r"^([A-Z]{2,3}):(.*)$")
    output = dict()
    current_key = None
    current = ""
    for line in inpt:
        line_match = reg.match(line) #check if we hit the CODE: Content line
        if line_match is not None:
            if current_key is not None:
                output[current_key] = current #if so - update the current_key with contents
            current_key = line_match.group(1)   
            current = line_match.group(2)
        else:
            current = current + line   #if it's not - it should be the continuation of previous key line
    
    output[current_key] = current #don't forget the last guy
    print(output)
    

