0

在条件语句运行后如何以编程方式停止 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") 

谢谢,迈克

4

4 回答 4

2
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()
于 2012-03-05T23:54:44.543 回答
1

我会这样做:

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()

任何文档

于 2012-03-06T00:35:38.780 回答
1

不确定我是否理解正确

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()
于 2012-03-05T23:58:10.573 回答
1

另一种方法:

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()
于 2012-10-04T11:29:09.583 回答