pythonlistmatchingorganizer

Matching items in a string


I am trying to work with a output I am receiving from a job application database, I need each account information organized into a list but I am not sure how match the applicants startdates because it starts at #1 but sometimes it's to be paired with a different number depending if the person was hired or not.

For example startdate#1 needs to be paired with name#3, and startdate#2 needs to be paired with name#4.

What I am starting with:

FirstName#1 = Joe | FirstName#2 = Michael | FirstName#3 = Harold | FirstName#4 = John | LastName#1 = Miles | LastName#2 = Gomez | LastName#3 = Hall | LastName#4 = Hancock | Hired#1 = False | Hired#2 = False | Hired#3 = True | Hired#4 = True | StartDate#1 = 10/31/2018 | StartDate#2 = 10/25/2018 |

Need to output:

[['Joe','Miles','False'], ['Michael','Gomez','False'], ['Harold','Hall','True','10/31/2018'], ['John','Hancock','True','10/25/2018']]

Solution

  • you may do this as below: (you may want to read about python 'exec')

    import re
    
    db_output = """FirstName#1 = Joe | FirstName#2 = Michael | FirstName#3 = Harold |        FirstName#4 = John | LastName#1 = Miles | LastName#2 = Gomez | LastName#3 = Hall | LastName#4 = Hancock | Hired#1 = False | Hired#2 = False | Hired#3 = True | Hired#4 = True | StartDate#1 = 10/31/2018 | StartDate#2 = 10/25/2018 |"""
    
    # define the keys
    keys = ['FirstName', 'LastName', 'Hired', 'StartDate']
    
    # create empty dict with name in keys
    for key in keys:
        exec('{} = dict()'.format(key))
    
    # parse and build dicts
    for data in db_output.split(' |'):
        data = data.strip()
        if data == '':
            continue
    
        reobj = re.search('(\S+)#(\d+)\s+=(.*)', data)
        if reobj:
            key = reobj.group(1).strip()
            num = int(reobj.group(2))
            value = reobj.group(3).strip()
    
            print key, num, value
            exec('{}[num] = value'.format(key))
    
    # get the inexes for FirstName and sort it
    exec('firstname_indexes = {}.keys()'.format(keys[0]))
    firstname_indexes = sorted(firstname_indexes)
    
    # get all local variables (required due to usage 'exec')
    local = locals()
    
    # variable to collect all data
    output = list()
    
    # Start date starts with 1
    startdate_track = 1
    
    # use firstname indexes and track start date, create a tmp list and
    # then append to output
    for fn_index in firstname_indexes:
        tmp = list()
        for key in keys:
            if key == 'StartDate':
                if tmp[-1] == 'True':
                    tmp.append(local[key][startdate_track])
                    startdate_track += 1
                output.append(tmp)
            else:
                tmp.append(local[key][fn_index])
    
    print output
    

    Output:

    [['Joe', 'Miles', 'False'], ['Michael', 'Gomez', 'False'], ['Harold', 'Hall', 'True', '10/31/2018'], ['John', 'Hancock', 'True', '10/25/2018']]