0

Could any one please help me converting the below IF conditions to a list. Each IF condition has one IF block, one 'then' (separated by comma) block and one 'else block'. Nothing really worked close so far.

single IF condition looks like this:

IF(IN(COL1, 'val'), COL2, COL3)

Real-time scenario has nested conditions like this

IF(IN(COL1, 'val1'), 'A', IF(COL2 = 'val2', IF(COL3 = 'val3', 'B', 'C'), 'D')

Looking for a nested list for every IF condition with 'then' and 'else' as separate items like these: ('then' and 'else' can be another IF statement that should follow the same listing process)

["IF(IN(COL1, 'val1')", 'A', ["IF(COL2 = 'val2'", ["IF(COL3 = 'val3'", 'B', 'C'], 'D']]

or

["IN(COL1 = 'val1'", 'A', ["COL2 = 'val2'", ["COL3 = 'val3'", 'B', 'C'], 'D']]
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Agv
  • 1
  • 2
    Can you demonstrate *any* effort at solving this yourself? – Scott Hunter Mar 08 '20 at 13:49
  • I would suggest having a look at the [formulas](https://pypi.org/project/formulas/) library... Don't reinvent the wheel (without a good reason) – Ed Ward Mar 08 '20 at 13:53
  • I was able to do this by replacing the braces comes with IN condition to curly braces first. Then I used this as reference to change all the IF statements into a corresponding lists. https://stackoverflow.com/questions/23185540/turn-a-string-with-nested-parenthesis-into-a-nested-list-python – Agv Mar 09 '20 at 15:37
  • with this one change in the code - def parse_nested(text, left=r'IF[(]', right=r'[)]', sep=r',(?![^{]*[}])'): – Agv Mar 09 '20 at 15:40

0 Answers0