首页 > 解决方案 > 找到一个短语/字符串并阅读与该短语/字符串对应的行

问题描述

这是文件的一部分:-

### 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 个条目。我想找到“port-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()

但是代码不起作用..还有其他方法可以编写代码..?谢谢!

标签: pythonpython-3.x

解决方案


这应该有效:

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)

推荐阅读