我在遍历具有值的数组的字符串中拆分,此拆分必须包含以下规则:
- 当有特殊字符时,将字符串拆分为两部分,并选择第一部分作为结果;
脚本
array = [
'srv1 #s',
'srv2;192.168.9.1'
]
result = []
for x in array:
outfinally = [line.split(';')[0] and line.split()[0] for line in x.splitlines() if line and line[0].isalpha()]
for srv in outfinally:
if srv != None:
result.append(srv)
for i in result:
print(i)
输出
srv1
srv2;192.168.9.1
期望的输出
srv1
srv2