Using RegEx to recompile a third list

Viewed 55

I have some code that recompile a list into 2 separate lists using RegEx and re.compile. I want to repurpose this to create 3 lists one for the name one for the error and one for the %.

List = ['701, HealthyPI 85%, CM01_Verify Camera Not Ready 4%, UnknownPI 3%, CM01_Label Printer Fault 1.91%, CM01_Takeaway Conveyor Motor Fault 0.01%, UnknownPI|UnknownPI 0.00%, CM01_Kickout Conveyor Motor Fault 0.00%,,703, HealthyPI 48%, CM01_Label Printer Fault 0.84%, CM01_Grip_Servo_Axis_Fault 0.26%, UnknownPI 0.00%, CM01_Kickout Conveyor Motor Fault 0.00%,,704, HealthyPI 81%, CM01_Front Side Jaw Access Door Open 0.29%, UnknownPI 0.06%, CM01_Kickout Conveyor Motor Fault 0.01%,,705, HealthyPI 39%, CM01_Label Printer Fault 1.25%, CM01_Bubble Film Roll Out of Position 0.83%, CM01_Sealing Jaw Not Clear 0.02%, UnknownPI 0.00%,,706, HealthyPI 42%, CM01_Label Printer Fault 1.45%, UnknownPI 0.00%,,707, HealthyPI 44%, CM01_Label Printer Fault 1.36%, CM01_Bubble Film Roll Out of Position 0.91%, CM01_Nip Roller Retracted during Run Attempt 0.13%, CM01_Product Sensor Not Clear 0.03%, UnknownPI 0.00%,,708, CM01_Verify Camera Not Ready 13%,,710, HealthyPI 33%, CM01_Bubble Film Roll Out of Position 1.14%, UnknownPI 0.00%,,712, HealthyPI 10%, CM01_Label Printer Fault 2%, UnknownPI 0.00%,,713, HealthyPI 21%, CM01_Nip Roller Retracted during Run Attempt 0.19%, CM01_Back Side Exit Conveyor 
E-Stop Pressed 0.02%, UnknownPI 0.00%, CM01_Kickout Conveyor Motor Fault 0.00%,']

Current Code :

  list_1 = ['SmartPac']
  list_2 = ['Availability']
  for string in list:
   temp = re.compile("(,|^)((\d+)(\.\d+)?)(,|$)").findall(string)
   list_1 += [x[1] for x in temp]
   temp = re.compile("(,|^)((\d+)(\.\d+)?%)(,|$)").findall(string)
   list_2 += [x[1] for x in temp]

Desired output:

List_Name = ['701', '703', '704', '705', '706', '707', '708', '710', '712', '713']
List_ERROR = ['HealthyPI','CM01_Verify Camera Not Ready','UnknownPI',etc]
List_% = ['85%','4%','3%',etc]

I want to be able to complete a table by column of list one . There can be more then one error per equipment name and the follow %.

1 Answers

Use this code:

import re
t = ['701, HealthyPI 85%, CM01_Verify Camera Not Ready 4%, UnknownPI 3%, CM01_Label Printer Fault 1.91%, CM01_Takeaway Conveyor Motor Fault 0.01%, UnknownPI|UnknownPI 0.00%, CM01_Kickout Conveyor Motor Fault 0.00%,,703, HealthyPI 48%, CM01_Label Printer Fault 0.84%, CM01_Grip_Servo_Axis_Fault 0.26%, UnknownPI 0.00%, CM01_Kickout Conveyor Motor Fault 0.00%,,704, HealthyPI 81%, CM01_Front Side Jaw Access Door Open 0.29%, UnknownPI 0.06%, CM01_Kickout Conveyor Motor Fault 0.01%,,705, HealthyPI 39%, CM01_Label Printer Fault 1.25%, CM01_Bubble Film Roll Out of Position 0.83%, CM01_Sealing Jaw Not Clear 0.02%, UnknownPI 0.00%,,706, HealthyPI 42%, CM01_Label Printer Fault 1.45%, UnknownPI 0.00%,,707, HealthyPI 44%, CM01_Label Printer Fault 1.36%, CM01_Bubble Film Roll Out of Position 0.91%, CM01_Nip Roller Retracted during Run Attempt 0.13%, CM01_Product Sensor Not Clear 0.03%, UnknownPI 0.00%,,708, CM01_Verify Camera Not Ready 13%,,710, HealthyPI 33%, CM01_Bubble Film Roll Out of Position 1.14%, UnknownPI 0.00%,,712, HealthyPI 10%, CM01_Label Printer Fault 2%, UnknownPI 0.00%,,713, HealthyPI 21%, CM01_Nip Roller Retracted during Run Attempt 0.19%, CM01_Back Side Exit Conveyor E-Stop Pressed 0.02%, UnknownPI 0.00%, CM01_Kickout Conveyor Motor Fault 0.00%,']
rows = []
for s in t:
    entries = s.split(',,')
    for entry in entries:
        row = {}
        vals = entry.strip(',').split(',')
        row['id'] = vals[0]
        row['errors'] = [x.strip().rsplit(' ', maxsplit=1) for x in vals[1:]]
        rows.append(row)

print(rows)

Results:

[{'id': '701', 'errors': [['HealthyPI', '85%'], ['CM01_Verify Camera Not Ready', '4%'], ['UnknownPI', '3%'], ['CM01_Label Printer Fault', '1.91%'], ['CM01_Takeaway Conveyor Motor Fault', '0.01%'], ['UnknownPI|UnknownPI', '0.00%'], ['CM01_Kickout Conveyor Motor Fault', '0.00%']]}, {'id': '703', 'errors': [['HealthyPI', '48%'], ['CM01_Label Printer Fault', '0.84%'], ['CM01_Grip_Servo_Axis_Fault', '0.26%'], ['UnknownPI', '0.00%'], ['CM01_Kickout Conveyor Motor Fault', '0.00%']]}, {'id': '704', 'errors': [['HealthyPI', '81%'], ['CM01_Front Side Jaw Access Door Open', '0.29%'], ['UnknownPI', '0.06%'], ['CM01_Kickout Conveyor Motor Fault', '0.01%']]}, {'id': '705', 'errors': [['HealthyPI', '39%'], ['CM01_Label Printer Fault', '1.25%'], ['CM01_Bubble Film Roll Out of Position', '0.83%'], ['CM01_Sealing Jaw Not Clear', '0.02%'], ['UnknownPI', '0.00%']]}, {'id': '706', 'errors': [['HealthyPI', '42%'], ['CM01_Label Printer Fault', '1.45%'], ['UnknownPI', '0.00%']]}, {'id': '707', 'errors': [['HealthyPI', '44%'], ['CM01_Label Printer Fault', '1.36%'], ['CM01_Bubble Film Roll Out of Position', '0.91%'], ['CM01_Nip Roller Retracted during Run Attempt', '0.13%'], ['CM01_Product Sensor Not Clear', '0.03%'], ['UnknownPI', '0.00%']]}, {'id': '708', 'errors': [['CM01_Verify Camera Not Ready', '13%']]}, {'id': '710', 'errors': [['HealthyPI', '33%'], ['CM01_Bubble Film Roll Out of Position', '1.14%'], ['UnknownPI', '0.00%']]}, {'id': '712', 'errors': [['HealthyPI', '10%'], ['CM01_Label Printer Fault', '2%'], ['UnknownPI', '0.00%']]}, {'id': '713', 'errors': [['HealthyPI', '21%'], ['CM01_Nip Roller Retracted during Run Attempt', '0.19%'], ['CM01_Back Side Exit Conveyor E-Stop Pressed', '0.02%'], ['UnknownPI', '0.00%'], ['CM01_Kickout Conveyor Motor Fault', '0.00%']]}]
Related