找到一个短语/字符串并阅读与该短语/字符串对应的行



这是文件的一部分:-

### zones_list.txt file ########
------------------------
VSAN:1     FCID:0x6f01e0
------------------------
port-wwn (vendor)           :20:32:00:02:ac:02:74:24             
node-wwn                    :2f:f7:00:02:ac:02:74:24
class                       :3
node-ip-addr                :0.0.0.0
ipa                         :ff ff ff ff ff ff ff ff
fc4-types:fc4_features      :scsi-fcp:target 
symbolic-port-name          :4UW0002645 - 0:3:2 - LPE32004-32G
symbolic-node-name          :HPE_3PAR A650 - 4UW0002645 - fw:4300
port-type                   :N 
port-ip-addr                :0.0.0.0
fabric-port-wwn             :20:03:00:de:fb:ce:e9:40
hard-addr                   :0x000000
permanent-port-wwn (vendor) :20:32:00:02:ac:02:74:24             
connected interface         :fc1/3
switch name (IP address)    :c3-sn6610c-02 (15.112.42.197)
------------------------
VSAN:1     FCID:0x6f0200
------------------------
port-wwn (vendor)           :20:33:00:02:ac:07:e9:d5             
node-wwn                    :2f:f7:00:02:ac:07:e9:d5
class                       :3
node-ip-addr                :0.0.0.0
ipa                         :ff ff ff ff ff ff ff ff
fc4-types:fc4_features      :scsi-fcp:target 
symbolic-port-name          :4UW0002955 - 0:3:3 - LPE32004-32G
symbolic-node-name          :HPE_3PAR C630 - 4UW0002955 - fw:4210
port-type                   :N 
port-ip-addr                :0.0.0.0
fabric-port-wwn             :20:0f:00:de:fb:ce:e9:40
hard-addr                   :0x000000
permanent-port-wwn (vendor) :20:33:00:02:ac:07:e9:d5             
connected interface         :fc1/15
switch name (IP address)    :c3-sn6610c-02 (15.112.42.197)
------------------------
VSAN:1     FCID:0x8d0000
------------------------
port-wwn (vendor)           :10:00:00:10:9b:8c:26:64 (Emulex)    
node-wwn                    :20:00:00:10:9b:8c:26:64
class                       :3
node-ip-addr                :0.0.0.0
ipa                         :ff ff ff ff ff ff ff ff
fc4-types:fc4_features      :
symbolic-port-name          :
symbolic-node-name          :
port-type                   :N 
port-ip-addr                :0.0.0.0
fabric-port-wwn             :20:07:00:3a:9c:53:9e:b0
hard-addr                   :0x000000
permanent-port-wwn (vendor) :00:00:00:00:00:00:00:00             
connected interface         :fc1/7
switch name (IP address)    :c3-cs9148-44 (15.112.48.20)
------------------------

按照上述方式,该文件有100个条目。我想找到";端口wwn(供应商(:20:32:00:02:ac:02:74:24";并读出";连接接口";以及";交换机名称";。。因此,在我的代码中,我要求用户输入wwn编号";xx:xx:…:xx";我搜索该条目,找到第行的索引,将+13和+14添加到索引中,并打印该索引中的第13行和第14行。

下面的代码给了我对应于wwn"的行索引;xx:xx:…:xx";

with open("zones_list.txt", 'r') as f:
#lines = f.readlines()
for (i, line) in enumerate(f):
if wwn in line:
print("index is : " + str(i))
#j = i + 13
#k = i + 14
#print(lines[j])
#print(lines[k])
break
f.close()

但是,当我想打印与我想要的相位/字符串相对应的索引之后的第13行和第14行时,这没有任何帮助?

with open("zones_list.txt", 'r') as f:
lines = f.readlines()
for (i, line) in enumerate(f):
if wwn in line:
print("index is : " + str(i))
j = i + 13
k = i + 14            
print(lines[j])
print(lines[k])
break
f.close()

但是代码不起作用。。还有其他写代码的方法吗。。?非常感谢。

这应该有效:

wwn = input()
with open("zones_list.txt", 'r') as f:
lines = f.readlines()
for (i, line) in enumerate(lines):
if wwn in line:
print("index is : " + str(i))
j = i + 13
k = i + 14
print(lines[j])
print(lines[k])
break

输入:

20:32:00:02:ac:02:74:24

输出:

index is : 3
connected interface         :fc1/3
switch name (IP address)    :c3-sn6610c-02 (15.112.42.197)

最新更新