gpt4 book ai didi

python - 当键和值包含在现有字段的值中时读取它们

转载 作者:行者123 更新时间:2023-12-01 01:03:41 24 4
gpt4 key购买 nike

我有一个摘录,其中包含以下格式的几个 JSON 字符串:

{'assignedTo': 'a5060ed2', 'automated': 'Not Automated', 'build': None, 'configurationId': 123, 
'configurationName': 'Package 1.0', 'lastResultState': 1, 'lastRunBy': '', 'lastRunDuration': 0,
'mostRecentResultOutcome': 2, 'mostRecentRunId': 1234, 'outcome': 'Passed', 'state': 2,
'suiteId': 1234, 'suiteName': 'Name', 'testCaseId': 12345, 'testPointId': 12345, 'tester': 'Fred Smith',
'workItemProperties': [{'Key': 'System.Id', 'Value': 12345}, {'Key': 'System.Title', 'Value': 'Item Item'},
{'Key': 'System.IterationPath', 'Value': 'Path\Path'},
{'Key': 'System.ChangedDate', 'Value': '/Date(1554200489873)/'},
{'Key': 'System.ChangedBy', 'Value': 'Fred Smith'},
{'Key': 'Microsoft.VSTS.TCM.AutomationStatus', 'Value': 'Not Automated'}]}

我已经能够循环这些并将它们显示在 Pandas DataFrame 中,将每个字符串附加为新行,但我遇到了问题。我的 json 字符串中有一个字段列表:

assignedTo
etc
workItemProperties < - this is the last field in the list

最后一个字段“workItemProperties”的值如下:

[{'Key': 'System.Id', 'Value': 12345}, {'Key': 'System.Title', 'Value': 'Item Item'}, 
{'Key': 'System.IterationPath', 'Value': 'Path\Path'},
{'Key': 'System.ChangedDate', 'Value': '/Date(1554200489873)/'},
{'Key': 'System.ChangedBy', 'Value': 'Fred Smith'},
{'Key': 'Microsoft.VSTS.TCM.AutomationStatus', 'Value': 'Not Automated'}]

我希望能够在我的表中显示该值中保存的字段,因此我的字段列表如下所示:

assignedTo

workItemProperties
System.Id
System.Title
System.IterationPath
Etc

是否可以让 Pandas 从 workItemProperties 的值中拾取并识别这些“子”字段和值?或者我是否必须进行某种进一步的字符串提取/操作?

最佳答案

您可以使用json_normalize

例如:

from pandas.io.json import json_normalize

data = {'assignedTo': 'a5060ed2', 'automated': 'Not Automated', 'build': None, 'configurationId': 123,
'configurationName': 'Package 1.0', 'lastResultState': 1, 'lastRunBy': '', 'lastRunDuration': 0,
'mostRecentResultOutcome': 2, 'mostRecentRunId': 1234, 'outcome': 'Passed', 'state': 2,
'suiteId': 1234, 'suiteName': 'Name', 'testCaseId': 12345, 'testPointId': 12345, 'tester': 'Fred Smith',
'workItemProperties': [{'Key': 'System.Id', 'Value': 12345}, {'Key': 'System.Title', 'Value': 'Item Item'},
{'Key': 'System.IterationPath', 'Value': 'Path\Path'},
{'Key': 'System.ChangedDate', 'Value': '/Date(1554200489873)/'},
{'Key': 'System.ChangedBy', 'Value': 'Fred Smith'},
{'Key': 'Microsoft.VSTS.TCM.AutomationStatus', 'Value': 'Not Automated'}]}


df = json_normalize(data, "workItemProperties", ['lastRunDuration', 'tester', 'testPointId', 'lastResultState', 'configurationId', 'mostRecentRunId', 'suiteName', 'state', 'testCaseId', 'assignedTo', 'configurationName', 'suiteId', 'build', 'mostRecentResultOutcome', 'automated', 'outcome', 'lastRunBy'])
df["workItemProperties"] = df.pop("Key")
df.drop(["Value"], inplace=True, axis=1)
print(df)

输出:

   lastRunDuration  mostRecentResultOutcome      tester  configurationId  \
0 0 2 Fred Smith 123
1 0 2 Fred Smith 123
2 0 2 Fred Smith 123
3 0 2 Fred Smith 123
4 0 2 Fred Smith 123
5 0 2 Fred Smith 123

mostRecentRunId suiteName testCaseId lastResultState state suiteId \
0 1234 Name 12345 1 2 1234
1 1234 Name 12345 1 2 1234
2 1234 Name 12345 1 2 1234
3 1234 Name 12345 1 2 1234
4 1234 Name 12345 1 2 1234
5 1234 Name 12345 1 2 1234

build testPointId automated configurationName outcome assignedTo \
0 None 12345 Not Automated Package 1.0 Passed a5060ed2
1 None 12345 Not Automated Package 1.0 Passed a5060ed2
2 None 12345 Not Automated Package 1.0 Passed a5060ed2
3 None 12345 Not Automated Package 1.0 Passed a5060ed2
4 None 12345 Not Automated Package 1.0 Passed a5060ed2
5 None 12345 Not Automated Package 1.0 Passed a5060ed2

lastRunBy workItemProperties
0 System.Id
1 System.Title
2 System.IterationPath
3 System.ChangedDate
4 System.ChangedBy
5 Microsoft.VSTS.TCM.AutomationStatus

关于python - 当键和值包含在现有字段的值中时读取它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55573008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com