如何将WIQL查询输出转换为结构化数据帧



我已经通过他们的API连接到Azure DevOps(由于提出了这个问题(,并设法获得所需的输出,详细说明记录的所有工作项。但是,我想将输出转换为数据帧。

def print_work_items(work_items):
for work_item in work_items:
print(
"{0} {1}: {2}".format(
work_item.fields["System.WorkItemType"],
work_item.id,
work_item.fields["System.Title"],
)
)

wit_client = connection.clients.get_work_item_tracking_client()

def get_TC_from_query(query):
query_wiql = Wiql(query=query)
results = wit_client.query_by_wiql(query_wiql).work_items
# WIQL query gives a WorkItemReference => we get the corresponding WorkItem from id
work_items = (wit_client.get_work_item(int(result.id)) for result in results)
#print_work_items(work_items)


dq_workitems = get_TC_from_query(
"""
SELECT
[System.Id],
[System.WorkItemType],
[System.Title],
[System.State],
[System.AreaPath],
[System.AssignedTo]
FROM workitems
WHERE
[System.AreaPath] = 'Designated Area Path'
ORDER BY [System.Id] DESC
"""
)

df = pd.DataFrame(print_work_items(dq_workitems), columns=['Id','WorkItemType','Title',
'State','AreaPath','AssignedTo']) 
print(df)

我可以将工作项打印到控制台上,但无法填充数据帧。任何想法/线索都将不胜感激!

以下步骤对我有效:

  • 我将每一行转换为一个数据帧
  • 连接数据帧的列表
  • 将列表导出为CSV
work_items = (
wit_client.get_work_item(int(res.id)) for res in wiql_results
)
workitems_df_list: List[DataFrame] = []
for work_item in work_items:
row = "{0},{1},{2},{3},{4},{5},{6}".format(
work_item.id,
work_item.fields["System.TeamProject"],
work_item.fields["System.WorkItemType"],
work_item.fields["System.Title"],
work_item.fields["System.State"],
work_item.fields["Microsoft.VSTS.Common.Priority"],
work_item.fields["System.CreatedDate"]
)
workitems_df_list.append(pd.DataFrame(list(reader([row]))))
workitems_final_df = pd.concat(workitems_df_list)
workitems_final_df.to_csv(r'C:testworkitems_test.csv', sep=',')

我们可以尝试先将Wiql查询输出写入.csv文件,然后将csv文件转换为数据帧。

写入.csv文件:

# Create the csv file
with open('vehicle.csv', 'w', newline='') as f_handle:
writer = csv.writer(f_handle)
# Add the header/column names
header = ['id', 'Title', 'State', 'WorkItemType']
writer.writerow(header)
# Iterate over `data`  and  write to the csv file
for row in data:
writer.writerow(row)

将csv文件转换为数据帧:

使用pd.read_csv()将.csv文件转换为pandas数据帧。

以.csv文件的路径名作为文件调用pd.read_csv(file),以返回带有.csv数据的DataFrame。

以下是您可以参考的一些参考资料:

  • 查询输出到.csv
  • Python将csv文件转换为数据帧
  • 将csv文件转换为pandas数据帧

最新更新