我具有以下语法,但是只要在3.6中运行,总是会抛出错误: '_sre.SRE_Match'对象不可下标 脚本在python3.7中工作正常,但在python3.6中出错
if {host}.issubset(sg_data['hosts'].split(',')):
saved_sg = [x for x in recorded_state if x['sg'] == sg_data['id'] and x['host'] == host][0]['data']
dec_saved_data = json.loads(self.encryption.decrypt(saved_sg).decode())
if sg_data['display_state'].lower() == 'offline':
if dec_saved_data['display_state'].lower() != 'offline':
yield from self.set_online(sg_data, host)
else:
parsed_host = re.search('^.*\((.*)\).*$', sg_data['display_state'])
if sg_data['display_type'].lower() == 'parallel':
if parsed_host:
if not {host}.issubset(parsed_host.group(1).split(',')):
yield from self.set_online(sg_data, host)
else:
if dec_saved_data['display_state'].lower() == 'offline':
yield from self.set_offline(sg_data, host)
else:
parsed_saved_host = re.search('^.*\((.*)\).*$', dec_saved_data['display_state'])
if parsed_saved_host:
if not {parsed_host[0]}.issubset(parsed_saved_host.group(1).split(',')):
yield from self.set_switch(sg_data, host)
if dec_saved_data['frozen'] != sg_data['frozen']:
if dec_saved_data['frozen'] == 0 and sg_data['frozen'] > 0:
yield from self.set_unfreeze(sg_data)
elif dec_saved_data['frozen'] > 0 and sg_data['frozen'] == 0:
yield from self.set_freeze(sg_data)
current_sg_state = sg_data['display_state']
lastest_sg_state = dec_saved_data['display_state']
parsed_current_sg_state = re.search('^.*\((.*)\).*$', sg_data['display_state'])
parsed_lastest_sg_state = re.search('^.*\((.*)\).*$', dec_saved_data['display_state'])
if parsed_current_sg_state:
current_sg_state = parsed_current_sg_state.group(1)
if parsed_lastest_sg_state:
lastest_sg_state = parsed_lastest_sg_state.group(1)
答案 0 :(得分:2)
您的行在这里:
if not {parsed_host[0]}.issubset(parsed_saved_host.group(1).split(',')):
试图访问0
的项parsed_host
(一个Match
对象)。
由于Match.__getitem__()
方法是根据the documentation从Python 3.6开始实施的,因此您的代码应在Python 3.6中运行,并且如果遇到'_sre.SRE_Match' object is not subscriptable
错误,则意味着您正在实际上不是使用Python 3.6,而是早期版本。
将行更改为:
if not {parsed_host.group(0)}.issubset(parsed_saved_host.group(1).split(',')):
,该代码将适用于Python的早期版本。