繁体   English   中英

运行条件语句后以编程方式停止执行python脚本

[英]Programmatically stop execution of python script after running condition statement

条件语句执行完后,如何以编程方式停止python脚本。 在下面的伪脚本中:

for row in rows:

    if row.FIRSTDATE == row.SECONDDATE:
        pass
    else:
        print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID

## If I set my quit sequence at the this tab level, it quits after the first
## unmatched record is found. I don't want that, I want it to quit after all the
## unmatched records have been found, if any. if all records match, I want the
## script to continue and not quit

        sys.quit("Ending Script") 

谢谢,迈克

quit_flag = False
for row in rows:

    if row.FIRSTDATE == row.SECONDDATE:
        pass
    else:
        print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
        quit_flag = True

if quit_flag:
    print "Ending Script"
    sys.exit()

另一种方法:

mis_match = []

for row in rows:
    if row.FIRSTDATE != row.SECONDDATE:
        mis_match.append(row.UNIQUEID)

if mis_match:
  print "The following rows didn't match" + '\n'.join(mis_match)
  sys.exit()

不知道我是否正确理解

doQuit = 0
for row in rows:
    if row.FIRSTDATE != row.SECONDDATE:
        print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
        doQuit = 1
if doQuit: sys.exit()

我会这样做:

def DifferentDates(row):
    if row.FIRSTDATE != row.SECONDDATE:
        print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
        return True
    else:
        return False

# Fill a list with Trues and Falses, using the check above
checked_rows = map(DifferentDates, rows)

# If any one row is different, sys exit
if any(checked_rows):
    sys.exit()

任何文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM