Python list to insert to other table or list
Question: Hi pythonist I am newbie here. I have a table with duplicate date and field2
as id of sale

table_sale

    
    
    field1      field2  field3                  field4  field5                  
    3/16/2012   a       KONDRA I KOMANG         1       TERAPI OZON 60 MENIT    
    3/16/2012   b       WARTI NI WAYAN          1       TERAPI OZON 60 MENIT    
    3/16/2012   c       MARDIKA I GUSTI PUTU    1       TERAPI OZON 60 MENIT    
    3/16/2012   d       DARMIASIH NI KOMANG     1       TERAPI OZON 60 MENIT    
    3/19/2012   e       DARMIASIH NI KOMANG     0.5     Orbitalized 240T        
    3/19/2012   e       DARMIASIH NI KOMANG     0.5     Octogenarian 240T       
    3/19/2012   e       DARMIASIH NI KOMANG     1       TERAPI AKUPUNKTUR       
    3/29/2012   f       ARNI NI MADE            3       Lingzhi 60C             
    3/29/2012   f       ARNI NI MADE            1       Octogenarian 240T   
    

How to get result or print like this: And do it in the python way? Thanks.

table_log

    
    
    field1                  field2                          
    KONDRA I KOMANG         ;3/16/2012 (1 TERAPI OZON 60 MENIT)
    WARTI NI WAYAN          ;3/16/2012 (1 TERAPI OZON 60 MENIT)
    MARDIKA I GUSTI PUTU    ;3/16/2012 (1 TERAPI OZON 60 MENIT)
    DARMIASIH NI KOMANG     ;3/16/2012 (1 TERAPI OZON 60 MENIT) ;3/19/2012 (0.5 Orbitalized 240T + 0.5  Octogenarian 240T + 1 TERAPI AKUPUNKTUR)
    ARNI NI MADE            ;3/29/2012 (3 Lingzhi 60C + 1 Octogenarian 240T)
    


Answer: This will parse your example table:

    
    
    from itertools import groupby
    from operator import itemgetter
    
    table = '''\
    field1      field2  field3                  field4  field5                  
    3/16/2012   a       KONDRA I KOMANG         1       TERAPI OZON 60 MENIT    
    3/16/2012   b       WARTI NI WAYAN          1       TERAPI OZON 60 MENIT    
    3/16/2012   c       MARDIKA I GUSTI PUTU    1       TERAPI OZON 60 MENIT    
    3/16/2012   d       DARMIASIH NI KOMANG     1       TERAPI OZON 60 MENIT    
    3/19/2012   e       DARMIASIH NI KOMANG     0.5     Orbitalized 240T        
    3/19/2012   e       DARMIASIH NI KOMANG     0.5     Octogenarian 240T       
    3/19/2012   e       DARMIASIH NI KOMANG     1       TERAPI AKUPUNKTUR       
    3/29/2012   f       ARNI NI MADE            3       Lingzhi 60C             
    3/29/2012   f       ARNI NI MADE            1       Octogenarian 240T
    '''
    
    # Setup bounds for fields in fixed width table.
    fields = (0,12,20,44,52,None)
    bounds = zip(fields[:-1],fields[1:])
    
    # Parse the data
    data = []
    for line in table.splitlines():
        data.append([line[a:b].rstrip() for a,b in bounds])
    
    # Sort the data by field3,field2 for groupby.
    data.pop(0) # remove header line
    data.sort(key=itemgetter(2,1))
    
    # Output result header
    print '{:24}{}'.format('field1','field2')
    
    # Output result lines
    for field3,items in groupby(data,key=itemgetter(2)):
        result = []
        for field1,field1_items in groupby(items,key=itemgetter(0)):
            result.append(';{} ({})'.format(field1,' + '.join(' '.join(item[3:]) for item in field1_items)))
        print '{:24}{}'.format(field3,' '.join(result))
    

### Output

    
    
    field1                  field2
    ARNI NI MADE            ;3/29/2012 (3 Lingzhi 60C + 1 Octogenarian 240T)
    DARMIASIH NI KOMANG     ;3/16/2012 (1 TERAPI OZON 60 MENIT) ;3/19/2012 (0.5 Orbitalized 240T + 0.5 Octogenarian 240T + 1 TERAPI AKUPUNKTUR)
    KONDRA I KOMANG         ;3/16/2012 (1 TERAPI OZON 60 MENIT)
    MARDIKA I GUSTI PUTU    ;3/16/2012 (1 TERAPI OZON 60 MENIT)
    WARTI NI WAYAN          ;3/16/2012 (1 TERAPI OZON 60 MENIT)
    

