Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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-AttributeError-如何正确强制使用现有异常_Python_Exception_Attributeerror - Fatal编程技术网

Python-AttributeError-如何正确强制使用现有异常

Python-AttributeError-如何正确强制使用现有异常,python,exception,attributeerror,Python,Exception,Attributeerror,我发现了错误(路径有点模糊): 代码部分如下: result = re.search('(http.*?.+txt)',url) url = result.group(1) 因此,我得到了一行,其中包含一个url。我尝试提取以http或https开头,以.txt结尾的URL,其中至少包含一个字符或-或uuu或。介于两者之间 问题是,有时我会出现上述错误。我想这意味着搜索没有成功。 我可以问一下result.group()是否可能,如果不可能,让它运行到我的一个标准异常中吗?最后一部分是关键问题

我发现了错误(路径有点模糊):

代码部分如下:

result = re.search('(http.*?.+txt)',url)
url = result.group(1)
因此,我得到了一行,其中包含一个url。我尝试提取以http或https开头,以.txt结尾的URL,其中至少包含一个字符或-或uuu或。介于两者之间

问题是,有时我会出现上述错误。我想这意味着搜索没有成功。 我可以问一下result.group()是否可能,如果不可能,让它运行到我的一个标准异常中吗?最后一部分是关键问题

except: 
  status = 'error'
  submitError2DB(db, fullurl, status, host, ip, txtname)
  print "\tAn unknown error occured. Fix needed.\nFailed.\n"
我只找到了一个很好的例子:

raise Exception("I know python!")

但那不是我需要的

您可以检查结果是否与此不同

if result is not None:
    url = result.group(1)
else:
    raise Exception("No match")

用你的话说,如果结果不是None,
result.group
是可能的,否则将引发文本
No match
异常。

只需检查
result
是否为
None
,就更容易、更简单了。太棒了!但是我如何将它重定向到我已经存在的异常中,并让它继续下一个消息(检查URL的下一行)?
if result is not None:
    url = result.group(1)
else:
    raise Exception("No match")