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

2024-10-02 02:24:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个摘录,其中包含以下格式的几个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'}]}

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

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

最后一个字段“WorkTempProperties”的值为:

[{'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

有没有可能让panda从worktemproperty的值中提取并识别这些“子”字段和值?或者我需要做一些进一步的字符串提取/操作吗


Tags: pathkey字符串idtitlevaluenotfred
2条回答

您可以使用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  

公认的答案非常有效,但有人向我建议的另一种选择也很有效:

for item in df['workItemProperties']:
    key = item['Key']
    df[key] = item['Value']
del dfheader['workItemProperties']
table = pd.DataFrame(df,index=[0])

这会将子字段和值与其余数据一起完全展平到列中

相关问题 更多 >

    热门问题