簡體   English   中英

在文件中的字符串匹配后,如何提取下一組行並繼續迭代

[英]After a match of a string in a file, how to extract the next set of lines and continue iteration

Sample data:-

Group Information:
Name                      Target     Status   Role       Mode     Options
SG_hpux_vgcgloack.r518634 s2976      Started  Primary    Sync     auto_recover,auto_failover,path_management,auto_synchronize,active_active
  LocalVV              ID   RemoteVV             ID   SyncStatus    LastSyncTime
  vgcglock_SG_cluster 13496 vgcglock_SG_cluster 28505 Synced        NA

Name                Target     Status   Role       Mode     Options
aix_rcg1_AA.r518634 s2976      Started  Primary    Sync     auto_recover,auto_failover,path_management,auto_synchronize,active_active
  LocalVV         ID   RemoteVV      ID   SyncStatus    LastSyncTime
  tpvvA_aix_r.2  20149 tpvvA_aix.2  41097 Synced        NA
  tpvvA_aix_r.3  20150 tpvvA_aix.3  41098 Synced        NA
  tpvvA_aix_r.4  20151 tpvvA_aix.4  41099 Synced        NA


Name                Target     Status   Role       Mode     Options
aix_rcg2_AA.r518634 s2976      Started  Primary    Sync     auto_recover,auto_failover,path_management,auto_synchronize,active_active
  LocalVV         ID   RemoteVV      ID   SyncStatus    LastSyncTime
  decoA_aix_r.11 20158 decoA_aix.11 41106 Synced        NA
  decoA_aix_r.12 20159 decoA_aix.12 41107 Synced        NA
  decoA_aix_r.13 20160 decoA_aix.13 41108 Synced        NA

我想搜索“名稱”行和緊接的下一行並將其用作鍵:值。

代碼:-

##The file is large and the code not shown here extract the data from Group Information line
##and saves to "no_extra_lines.

# here i am removing the empty lines or empty strings
no_extra_lines = [line for line in required_lines if line.strip() != ""]
print(no_extra_lines)
print(len(no_extra_lines))


#here i want to iterrate over the string and want to extract the line "Name" and the immedite next line.
for num, line in enumerate(no_extra_lines):
    print(num, line)
    if "Name" in line:
        print(line)
        print(line +1)  
    

如何打印行和下一行? 或者換句話說,如何在每次出現“名稱”后提取下一組行。 該列表很大,具有相同的模式。 我想為每次出現提取這兩行並保存為鍵值。

您應該使用num提取下一行,它是當前行的索引

for num, line in enumerate(no_extra_lines):
    print(num, line)
    if "Name" in line:
        print(line)
        print(no_extract_lines[num + 1])

在字符串print(line +1)中,您試圖向字符串添加一個數字,並且您需要從列表中獲取當前值之后的下一個值。 你可以這樣做:

if "Name" in line:
    print(line) 
    print(no_extra_lines[num + 1]

既然你說過“列表很大”,我建議使用一種只使用迭代並避免索引的方法:

def extract_name_next(lines):
    it = (line for line in lines if line.strip() != '')
    for line in it:
        if line.startswith('Name'):
            key = line
            try:
                value = next(it)
                yield key, value
            except StopIteration:
                raise ValueError('Name must be followd by additional line')

這是生成(鍵,值)對的生成器 function。 要打印,您可以像

for key, value in extract_name_next(required_lines):
    print(key)
    print(value)

您還可以從這些對構建字典:

mydict = dict(extract_name_next(required_lines))

請注意,您還可以使用此生成器從文件中提取鍵值對,即使該文件不適合 RAM:

with open('huge_file') as file:
    mydict = dict(extract_name_next(file))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM