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

标签 python json python-3.x pandas

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

相关文章:

python - 你能重载 Python 3.6 f-string 的 "operator"吗?

python - 如何从 Python 中的 json 文件一起调用变量和字符串?

python - 为什么一个代码(matmul)比另一个(Python)快

python - 如何将 Python dict 转换为特定类型的对象?

iPhone : Which type of parser recommended for data parsing between iPhone and web-service?

java - JAXB:解码异构数组

python - 没有为第二个单元测试定义全局名称

python - Pandas 对单元格中的字符串进行排序

json - 如何在 Go 中解析普通和引用的 JSON 数字?

mysql - 如何解决 django.db.utils.IntegrityError : (1364, "Field ' name' doesn't have a default value")