【发布时间】:2020-10-23 16:42:27
【问题描述】:
我正在使用 try 和 except 来防止 ValueError,但我仍然得到它,我不明白:
if ('Address' and 'Phone') in text_string:
try:
pos_1 = text_string.index('Address')
pos_2 = text_string.index('Phone')
output['address'] = text_string[pos_1+7:pos_2]
except ValueError:
output['address'] = None
我在这里收到 ValueError:
pos_1 = text_string.index('Address')
但是由于我已经过滤了 if 子句,我应该不会收到 ValueError 消息?
【问题讨论】:
-
试试
if 'Address' in text_string and 'Phone' in text_string:
标签: python-3.x try-catch valueerror except