簡體   English   中英

如何在兩個字符串標記python之間捕獲數據

[英]How to capture data between two string markers python

以下是我的日志文件中的一些內容

-- TEST DONE!!! --
-------------
Diff Coverage
Diff: origin/
-------------
a1
a2
-------------
Total:   54 lines
Missing: 41 lines
Coverage: 24%
-------------
Source files without UT:
1. 
2. 
3. 
4. 
5. 
6. 
7. 

我想捕獲“ -----------”標記之間的內容。內容將被追加到名為List = []的列表中,例如:List = ['Diff Coverage','Diff:origin /','a1','a2','總計:........]

我嘗試使用循環獲取這些內容,

 while True:
                Line=RF.readline()
                Line=Line.strip('\n')
                if not Line:
                        continue

                if Line == r"-- TEST DONE!!! --":
                        continue

                if Line == r"-------------":
                        while True:
                                Line=RF.readline()
                                Line=Line.strip('\n')
                                print(Line)

                                if re.search(r"Total:", Line) is not None:
                                        List.append(Line)
                                if re.search(r"Missing:", Line) is not None:
                                        List.append(Line)
                                if re.search(r"Coverage:", Line) is not None:
                                        List.append(Line)
                                if Line == r"Source files without UT:":
                                        List.append(Line)
                                        break

我有點想獲取所有數據。 感謝您的幫助

只需從日志中刪除以“-”開頭的行並將其附加到列表中即可。

data = []

with open('test') as f:
    for line in f:
        if not line.startswith('--'):
            data.append(line)

print(data)

這是一線解決方案。

碼:

with open(path_to_logfile) as file:
    print ([a.strip() for a in file.readlines() if not a.startswith("--")])
    #This will return list of values removing starting char "--" and strips \n

輸出:

['Diff Coverage', 'Diff: origin/', 'a1', 'a2', 'Total: 54 lines', 'Missing: 41 lines', 'Coverage: 24%', 'Source files without UT:', '1.', '2.', '3.', '4.', '5.', '6.', '7.']

友情鏈接

https://www.geeksforgeeks.org/python-list-comprehension-and-slicing/

您可以使用以下代碼來處理txt文件數據。 該代碼涵蓋:

  1. 單個文件中包含多個塊(“測試完成”,“總計”,“缺失”,“覆蓋率”)。
  2. 如果循環值順序將更改,則此代碼將不起作用。
  3. 我假設“總計”,“缺失”和“覆蓋率”將始終有一個數字(0或正值)。

代碼

import re

#Get the file content into list
with open ("yourFile.txt", "r") as myfile:
    log_messages_list = myfile.readlines()

#Looping true lines (when find "TEST DONE" in line continue with "Total:, Missing:, Coverage:")
foundDone = False
tmpDict = {}  #It will hold temporary values as {'Total': number, 'Missing': number, 'Coverage': String}
for line in log_messages_list:    
    if foundDone == False:
        if "TEST DONE" in line:
            foundDone = True
    else:
        if "Total:" in line:
            tmpDict['Total'] = int(re.findall(r'\d{1,}', line))
        elif "Missing:" in line:
            tmpDict['Missing'] = int(re.findall(r'\d{1,}', line))
        elif "Coverage:" in line:
            tmpDict['Coverage'] = str(re.findall(r'\d{1,}%', line))
            foundDone = False
            yourMasterList.append(tmpDict) #Put all results into a master list
            tmpDict.clear()

輸出

[{'Total': 54, 'Missing': 41, 'Coverage': '24%'}]

暫無
暫無

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

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