Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在错误库之后继续_Python_Loops - Fatal编程技术网

Python:在错误库之后继续

Python:在错误库之后继续,python,loops,Python,Loops,我有一个循环,它逐行读取文件并调用库。但是,有时库会发出自己的错误消息,然后整个循环停止工作,因为它终止了循环。有没有一种方法可以控制库中的消息?收到此错误消息时,如何使循环继续(即,如何检查此错误消息是否存在,以便跳过它) 我得到的错误是: raise EchoNestAPIError(code, message, headers, http_status) pyechonest.util.EchoNestAPIError: (u'Echo Nest API Error 5: The iden

我有一个循环,它逐行读取文件并调用库。但是,有时库会发出自己的错误消息,然后整个循环停止工作,因为它终止了循环。有没有一种方法可以控制库中的消息?收到此错误消息时,如何使循环继续(即,如何检查此错误消息是否存在,以便跳过它)

我得到的错误是:

raise EchoNestAPIError(code, message, headers, http_status)
pyechonest.util.EchoNestAPIError: (u'Echo Nest API Error 5: The identifier specified does not exist [HTTP 200]',)
这是库中处理错误的代码部分:

class EchoNestAPIError(EchoNestException):
    """
    API Specific Errors.
    """
    def __init__(self, code, message, headers, http_status):
        if http_status:
            http_status_message_part = ' [HTTP %d]' % http_status
        else:
            http_status_message_part = ''
        self.http_status = http_status

        formatted_message = ('Echo Nest API Error %d: %s%s' %
                             (code, message, http_status_message_part),)
        super(EchoNestAPIError, self).__init__(code, formatted_message, headers)


class EchoNestIOError(EchoNestException):
    """
    URL and HTTP errors.
    """
    def __init__(self, code=None, error=None, headers=headers):
        formatted_message = ('Echo Nest IOError: %s' % headers,)
        super(EchoNestIOError, self).__init__(code, formatted_message, headers)

def get_successful_response(raw_json):
    if hasattr(raw_json, 'headers'):
        headers = raw_json.headers
    else:
        headers = {'Headers':'No Headers'}
    if hasattr(raw_json, 'getcode'):
        http_status = raw_json.getcode()
    else:
        http_status = None
    raw_json = raw_json.read()
    try:
        response_dict = json.loads(raw_json)
        status_dict = response_dict['response']['status']
        code = int(status_dict['code'])
        message = status_dict['message']
        if (code != 0):
            # do some cute exception handling
            raise EchoNestAPIError(code, message, headers, http_status)
        del response_dict['response']['status']
        return response_dict
    except ValueError:
        logger.debug(traceback.format_exc())
        raise EchoNestAPIError(-1, "Unknown error.", headers, http_status)
我尝试在没有定义任何内容的情况下使用一个通用的“except”,这适用于我达到API限制的情况,但仍然不适用于我提出这个问题的错误。这个错误似乎来自同一个类。我不知道为什么它对限制错误有效,但对其他错误无效。以下是API限制的错误:

raise EchoNestAPIError(code, message, headers, http_status)
pyechonest.util.EchoNestAPIError: (u'Echo Nest API Error 3: 3|You are limited to 120 accesses every minute. You might be eligible for a rate limit increase, go to http://developer.echonest.com/account/upgrade [HTTP 429]',)

在一个块中捕获异常

例如:

with open(your_file, "r") as f:
    for line in f:
        try:
            api_call(line)
        except pyechonest.util.EchoNestAPIError:
            pass # or continue if you wish to skip processing this line.
try
-块中执行的每一行代码都可能导致异常,然后在
块中“捕获”异常,除了
-块(最后还有一个
块,在文档中有更多的内容)。上面的示例只是抑制异常,但这可能不是理想的解决方案


异常是该语言的一个基本功能,您至少应该阅读开始时的说明。

这就是错误处理的方法。阅读感谢,我尝试了类似的方法,但它不会传递错误消息(您的也不起作用)。发生的情况是,它打印出错误,然后停止。我认为问题不在于如何跳过它,而在于如何检测导致问题的错误消息。它似乎不是为了通过或继续循环而捕获它(我在异常部分尝试打印一些文本,但它从未打印)。我还尝试在文件顶部添加“from pyechonest import util”或except部分的变体(例如“util.EchoNestAPIError”)@user40037除非您显示有问题的代码,否则无法知道问题所在。我将其添加到了我的原始问题中:-)