【问题标题】:Python CSV from JIRA with duplicate Sprint headers来自 JIRA 的 Python CSV,带有重复的 Sprint 标头
【发布时间】:2020-02-07 05:45:51
【问题描述】:

我正在使用 python 3.7.5

我有一个 CSV 文件,我从 Jira 实例中取出该文件,以便查看哪个问题在哪个 sprint 中完成。 Jira 会跟踪问题所在的每个 sprint,因此如果您导出 CSV,您将获得多个 Sprint 标头,其中包含如下数据:

Issue key,Issue Type,Status,Sprint,Sprint,Sprint,Sprint
OLS-526,Story,Done,Sprint #16,Sprint #17,Sprint #18,Sprint #19
OLS-871,Story,Done,Sprint #18,Sprint #28,,
OLS-165,Story,Done,Sprint 1,Sprint 3,Sprint #18,Sprint #19
OLS-868,Story,Done,Sprint #28,,,

我需要确定问题是在哪个 Sprint 中交付的,所以最右边的 Sprint 列,这样我就可以计算实际有多少问题在每个 sprint 中完成。

我尝试过使用默认的 python 'csv' 和这样的 DictReader:

import csv
with open('../OLS-tix2.csv', newline='') as csvfile:
  reader = csv.DictReader(csvfile)
  for row in reader:
    print(row['Sprint'])

但您只能获得 last Sprint 列,如果该列中没有任何内容,则为空白。因为上面的输出看起来像这样:

Sprint #19

Sprint #19


我可以只使用普通的 csv 阅读器并自己滚动,但我认为在 python 中必须有更好的方法。

【问题讨论】:

    标签: python python-3.x csv jira


    【解决方案1】:

    好的,所以我环顾四周,发现pandas 看起来可能是完成这项工作的好工具。 有很多例子,作为奖励,我可以使用 dataframespivot_tableaggregating counts of ids 进行过滤/旋转

    这就是我最终为我工作的:

    import pandas as pd
    
    
    csv_file = "../OLS-tix.csv" # where the file is at
    ols_df = pd.read_csv(csv_file)
    finish_sprint_col = 'finish_sprint' # the column to put the actual Sprint thie issue was finished in
    ols_df[finish_sprint_col] = "" # add the new blank column
    sprints = ols_df.columns[ols_df.columns.str.contains('Sprint')] # get all the headers that contain the word sprint as they will be Sprint, Sprint.1 ... Sprint.N
    for i,row in ols_df.iterrows():
      if not ols_df.at[i,"Status"] == "Done": # we only want to do this for "Done" Issues
        continue
      finish_sprint = False
      for header in sprints: # go through all the sprint cells for this row and get the last not empty one.
        if not pd.isnull(ols_df.loc[i, header]):
          finish_sprint = ols_df.loc[i, header]
      if finish_sprint:
        ols_df.at[i,finish_sprint_col] = finish_sprint
    
    # get number of issue finished per sprint.
    dones = ols_df[(ols_df.Status == "Done") & (ols_df['Issue Type'] == "Story") ].pivot_table(index=["finish_sprint"],values=["Issue key"], aggfunc=[pd.Series.nunique])
    

    这可能是一种更简单的方法,但现在似乎可行......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 2020-04-14
      • 2016-06-21
      • 1970-01-01
      相关资源
      最近更新 更多