求助各位python大佬,自己想了很多办法没解决
[('rule 37 name S07100275', ''), ('', 'service telnet'), ('', 'service tcp_20060'), ('rule 38 name S7100275-2', ''), ('', 'telnet'), ('', 'service tcp_20060'), ('rule 36 name cy_test', ''), ('', 'service http'), ('', 'service https')]这是一个python元组,如何用python脚本得到下面的结果:
rule 17 name 20200325
service telnet
rule 27 name 202321321
service telnet
data = [('rule 37 name S07100275', ''), ('', 'service telnet'), ('', 'service tcp_20060'), ('rule 38 name S7100275-2', ''), ('', 'telnet'), ('', 'service tcp_20060'), ('rule 36 name cy_test', ''), ('', 'service http'), ('', 'service https')]
result = []
current_rule = None
for item in data:
if item.startswith('rule'):
current_rule = item
elif item.startswith('service telnet'):
result.append((current_rule, item))
for rule, service in result:
print(f'{rule}\n{service}')
cv搞定 本帖最后由 baigei2333 于 2023-9-27 11:14 编辑
我今天是大佬 发表于 2023-9-27 11:08
data = [('rule 37 name S07100275', ''), ('', 'service telnet'), ('', 'ser ...
非常感谢大佬。。刚刚发现有个地方漏了 我今天是大佬 发表于 2023-9-27 11:08
data = [('rule 37 name S07100275', ''), ('', 'service telnet'), ('', 'ser ...
想请教一下cv是啥的? baigei2333 发表于 2023-9-27 11:15
想请教一下cv是啥的?
复制粘贴 这种文本不多的情况,用文本文档,查找替换就可以完成了。 我今天是大佬 发表于 2023-9-27 11:15
复制粘贴
额,好吧 感谢大佬 \$\$\$\$\$\$ lst = [('rule 37 name S07100275', ''), ('', 'service telnet'), ('', 'service tcp_20060'), ('rule 38 name S7100275-2', ''), ('', 'telnet'), ('', 'service tcp_20060'), ('rule 36 name cy_test', ''), ('', 'service http'), ('', 'service https')]
result = []
for i in range(0, len(lst), 3):
rule_name = ''
service_name = ''
if 'name' in lst:
rule_name = lst
else:
service_name = lst
if 'name' in lst:
rule_name = lst
else:
service_name = lst
if 'name' in lst:
rule_name = lst
else:
service_name = lst
if rule_name != '' and service_name != '':
result.append(rule_name + '\n' + service_name)
print('\n'.join(result))
输出结果:
rule 37 name S07100275
service telnet
rule 38 name S7100275-2
telnet
rule 36 name cy_test
service http 看不懂,你这数据源跟结果是对应的吗。。。
页:
[1]
2