I have a multiline string sample below, which has a table-like structure. I have to parse that structure and convert back to key-value pairs such that the key is the column header and the value is the value of that row. I have used a regex but it's not working properly/
PFB string:
Number of Critical alarms: 0
Number of Major alarms: 0
Number of Minor alarms: 0
Slot Sensor Current State Reading Threshold(Minor,Major,Critical,Shutdown)
---------- -------------- --------------- ------------ ---------------------------------------
P0 PEM Iout Normal 5 A na
P0 PEM Vout Normal 12 V DC na
P0 PEM Vin Normal 242 V AC na
P0 Temp: PEM In Normal 34 Celsius (80 ,90 ,95 ,100)(Celsius)
P0 Temp: PEM Out Normal 30 Celsius (80 ,90 ,95 ,100)(Celsius)
R0 Temp: FC FANS Fan Speed 60% 23 Celsius (25 ,35 ,0 )(Celsius)
P0 Temp: FC FAN0 Fan Speed 60% 23 Celsius (25 ,35 ,0 )(Celsius)
P1 Temp: FC FAN1 Fan Speed 60% 23 Celsius (25 ,35 ,0 )(Celsius)
Expected Output:
[{'Slot': 'P0', 'Sensor': 'PEM Iout', 'Current State': 'Normal', 'Reading': '5 A', 'Threshold': 'na'}, ...]
I have used the below regex pattern:
r'^(?P<Slot>[^\s]+)[ \t]+(?P<Sensor>[a-zA-Z0-9:]+ [a-z0-9A-Z.:-]* [a-z0-9]*)[ \t]+(?P<State>[a-zA-Z]*)[ \t]+'