简体   繁体   English

循环在一次迭代后意外停止

[英]Loop unexpectantly stops after one iteration

I'm iterating over a list of lists with length 4, but it stops after one iteration. 我正在遍历一个长度为4的列表,但是在一次迭代之后它停止了。

        print((data))
        formatted_exec_dictionary_list = []
        for entry in data:
            print(entry)
            try:
                formatted_exec = {
                    'minute': self.validate_type_and_range(entry[0], 0, 59),
                    'hour': self.validate_type_and_range(entry[1], 0, 23),
                    'path': entry[2]
                }
                formatted_exec_dictionary_list.append(formatted_exec)
            except IndexError as exception:
                print(exception)
                if self.logger:
                    self.logger.error("""
                    Could not successfully parse entry...
                    Exception: %s""", exception)

In the topmost print in the snippet this is the output: 在代码段的最上方打印中,这是输出:

[[u'30', u'1', u'/bin/run_me_daily'], [u'45', u'*', u'/bin/run_me_hourly'], 
 [u'*', u'*', u'/bin/run_me_every_minute'], 
 [u'*', u'19', u'/bin/run_me_sixty_times']]

But the for-loop wrapped print only returns the first entry, then it stops. 但是for循环包装的打印仅返回第一个条目,然后停止。

No exception seems to be thrown as the print isn't firing and the rest of the program functions fine. 似乎没有异常,因为没有打印图像,并且程序的其余功能正常。 It's as if all the rest of the entries in the list are just ignored... 好像列表中的所有其余条目都被忽略了...

Is this something common? 这很常见吗? Any clue why this would be happening? 任何线索为什么会这样?

For clarification NOTHING in particular happens in the loop that seems to be throwing an exception. 为了澄清起见,没有什么特别的事情发生在似乎抛出异常的循环中。 The function validate_type_and_range prints the expected data, but only for the one iteration. 函数validate_type_and_range打印预期数据,但仅打印一次迭代。

When I replace your 'validate_type_and_range(...)' with some strings, I get the following output: 当我用一些字符串替换“ validate_type_and_range(...)”时,得到以下输出:

[[u'30', u'1', u'/bin/run_me_daily'], [u'45', u'*',  u'/bin/run_me_hourly'], [u'*', u'*', u'/bin/run_me_every_minute'], 
[u'*', u'19', u'/bin/run_me_sixty_times']]
[u'30', u'1', u'/bin/run_me_daily']
[u'45', u'*', u'/bin/run_me_hourly']
[u'*', u'*', u'/bin/run_me_every_minute']
[u'*', u'19', u'/bin/run_me_sixty_times']

I think there is something going on in your 'validate_type_and_range' 我认为您的“ validate_type_and_range”中发生了某些情况

print((data))
    formatted_exec_dictionary_list = []
    for entry in data:
        print(entry)
        try:
            formatted_exec = {
                'minute': "foo",
                'hour': "bar",
                'path': entry[2]
            }
            formatted_exec_dictionary_list.append(formatted_exec)
        except IndexError as exception:
            print(exception)
            if self.logger:
                self.logger.error("""
                Could not successfully parse entry...
                Exception: %s""", exception)

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

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