遍历发行说明并计算每个日期的修复次数

Iterate through release notes and count fixes per date

希望你一切都好。我试图在发行说明中找到“修复”的每个实例,并获取日期(通常在上面一行)。

例如(发行说明部分):

Date:        : 2021-04-26
Comment:     : Fix wrong ...

我想获取“修复”事件的数量及其对应的日期。

示例输出:

Date: 2021-02-13
Fixes: 3
Date: 2021-01-18
Fixes: 1
Etc...

这是我一直在尝试的:

WANTEDdate = 14
count = 0
dates = []
with open(file_name) as searchfile:

for line in searchfile:

    left,sep,right = line.partition('Date')

    if sep:

        count = count + 1

        temp = ((right[:WANTEDdate]))

        # temp = ((left[BEFORE:]))

        temp = temp.strip()

        temp = temp.strip(" :")

        if len(temp) > 0:

            print(temp)

            dates.append(temp)

        #print("Fix",count,temp)

        #print(temp)



# test
lookup = "Fix"
with open(file_name) as myFile:

for num, line in enumerate(myFile, 1):

    if lookup in line:

        dateLine = num - 1

        print("Found at line:", num)

如有任何帮助,我们将不胜感激。提前致谢。

假设您的文件如下所示:

Date:        : 2021-04-26
Comment:     : Fix wrong 1

Date:        : 2021-04-26
Comment:     : ---

Date:        : 2021-04-26
Comment:     : Fix wrong 2

Date:        : 2021-04-27
Comment:     : Fix wrong 3

然后就可以使用re模块来解析了:

import re

with open("your_file.txt", "r") as f_in:
    data = f_in.read()

dates = re.findall(r"Date.*:\s*(.*)", data)
comments = re.findall(r"Comment.*:\s*(.*)", data)

out = {}
for d, c in zip(dates, comments):
    if "fix" in c.lower():
        out.setdefault(d, 0)
        out[d] += 1

for k, v in out.items():
    print("Date:", k)
    print("Fixes:", v)

打印:

Date: 2021-04-26
Fixes: 2
Date: 2021-04-27
Fixes: 1