我尝试使用Python对其进行规范化后分析旧的PL / 1源文件,以提高效率。条件太多了,但我无法找到有效减少这种情况的方法。像这样:
if cond == 'A'
| cond == 'B'
| cond == 'C'
| cond_ex == 'A_EX'
| cond == 'D'
| cond_ex == 'B_EX'
then;
我想改变和减少这样的代码:
if cond in ('A','B','C','D') | cond_ex in ('A_EX','B_EX') then;
你能告诉我使用python regex或text replace来解析这些代码的解决方案吗?
答案 0 :(得分:0)
您可以按照以下方式解析数据,并根据需要制作if语句。
>>> a = """if cond == 'A'
... | cond == 'B'
... | cond == 'C'
... | cond_ex == 'A_EX'
... | cond == 'D'
... | cond_ex == 'B_EX'
... then;"""
>>> b = a.replace("if", "").replace("then","").replace(";","").replace("|","").replace(" ","")
>>> c = b.split()
>>> print c
["cond=='A'", "cond=='B'", "cond=='C'", "cond_ex=='A_EX'", "cond=='D'", "cond_ex=='B_EX'"]