我试图将clienterror消息的输出与python中的字符串进行比较,但无法使其正常工作。 这可能吗
我尝试了三种方法来解决这个问题:(A)(我没有得到任何结果)
except ClientError as e:
a = "An error occurred (ValidationError) when calling the UpdateStack operation: Stack [name] does not exist"
b = "An error occurred (ValidationError) when calling the UpdateStack operation: No updates are to be performed."
if e is a:
print ('Stack [name] does not exist')
if e == b:
print ('No updates are to be performed')
(B)
print e[5]
IndexError: tuple index out of range
--
print (e[76:])
()
print (e[:-1])
()
(C) 我试着通过跑步来理解这个句子有多少部分
print (len(e))
但是我得到了“TypeError:ClientError类型的对象没有len()
所以。。。我真的不能将ClientError与字符串进行比较吗
不,不能将字符串与异常进行比较。它们不是同一类型的
is
仅当它们是同一对象时才会返回true。即使==
在这里也不起作用,因为如上所述,它们不是同一类型但是,您可以将异常强制转换为字符串,然后使用
in
查看它是否包含以下消息:只有当
str(e)
包含完整的错误消息时,in
检查才会起作用。如果没有,你需要以另一种方式获得信息。异常消息的处理方式取决于子类,我无权访问要测试的ClientError
对象;我在网上也找不到它的定义相关问题 更多 >
编程相关推荐