全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 python论坛
1146 1
2018-01-10
我有如下的json格式的数据,想要用python解析到csv,请大神帮忙看一下,电脑里面只有python标准库和numpy,pandas等常用的数据分析库:

[
  {
    "status": "changed",
    "dataset": {
      "id": "5a4b463c855d783af4f5f695",
      "name": "AE_E",
      "label": "1- ADVERSE EVENTS - Not Analyzed"
    },
    "details": {
      "variables": [
        {
          "variable": {
            "id": "5a4b4647855d783b494f9d3f",
            "name": "CPEVENT",
            "label": "CPEVENT"
          },
          "status": "changed",
          "details": {
            "r_type": {
              "new_value": "unary",
              "old_value": "factor"
            }
          },
          "message": "Variable with different R Type"
        },
        {
          "variable": {
            "id": "5a4b4647855d783b494f9d25",
            "name": "CPEVENT2",
            "label": "CPEVENT2"
          },
          "status": "changed",
          "details": {
            "r_type": {
              "new_value": "unary",
              "old_value": "binary"
            }
          },
          "message": "Variable with different R Type"
        },
        {
          "variable": {
            "id": "5a4b4647855d783b494f9d26",
            "name": "CP_UNSCHEDULED",
            "label": "CP_UNSCHEDULED"
          },
          "status": "changed",
          "details": {
            "r_type": {
              "new_value": "undefined",
              "old_value": "unary"
            }
          },
          "message": "Variable with different R Type"
        },
        {
          "variable": {
            "id": "5a4b4647855d783b494f9d02",
            "name": "VISIT_NUMBER",
            "label": "VISIT_NUMBER"
          },
          "status": "changed",
          "details": {
            "r_type": {
              "new_value": "unary",
              "old_value": "integer"
            }
          },
          "message": "Variable with different R Type"
        },
        {
          "variable": {
            "id": "5a4b4647855d783b494f9ccf",
            "name": "VISIT_NUMBER2",
            "label": "VISIT_NUMBER2"
          },
          "status": "changed",
          "details": {
            "r_type": {
              "new_value": "unary",
              "old_value": "binary"
            }
          },
          "message": "Variable with different R Type"
        }
      ],
      "many_visits": null
    }
  },
  {
    "status": "changed",
    "dataset": {
      "id": "5a4b465b855d783af4f5f737",
      "name": "AE_EQG2",
      "label": "2 - ADVERSE EVENTS- Not Analyzed"
    },
    "details": {
      "variables": [
        {
          "variable": {
            "id": "5a4b4666855d783b4b5175ce",
            "name": "ADVE_MEDDRA_SOC",
            "label": "SYSTEM ORGAN CLASS"
          },
          "status": "changed",
          "details": {
            "r_type": {
              "new_value": "character",
              "old_value": "factor"
            }
          },
          "message": "Variable with different R Type"
        }
      ],
      "many_visits": null
    }
  },
  {
    "status": "changed",
    "dataset": {
      "id": "5a4b467a855d783af4f5f7d7",
      "name": "AE_M",
      "label": "3- ADVERSE EVENTS MEDICATION ERROR - Not Analyzed"
    },
    "details": {
      "variables": [
        {
          "variable": {
            "id": "5a4b4682855d783b494f9dad",
            "name": "ADVE_MEDDRA_PT",
            "label": "PREFERRED TERM -PT-"
          },
          "status": "changed",
          "details": {
            "r_type": {
              "new_value": "character",
              "old_value": "factor"
            }
          },
          "message": "Variable with different R Type"
        },
        {
          "variable": {
            "id": "5a4b4682855d783b494f9d90",
            "name": "ADVE_MEDDRA_PT_CODE",
            "label": "PREFERRED TERM -PT- CODE"
          },
          "status": "changed",
          "details": {
            "r_type": {
              "new_value": "character",
              "old_value": "factor"
            }
          },
          "message": "Variable with different R Type"
        }
      ],
      "many_visits": null
    }
  },
  {
    "status": "unchanged",
    "dataset": {
      "id": "5a4b468c855d783af4f5f839",
      "name": "AGG_AE_E",
      "label": "1.1 - ADVERSE EVENTS- Aggregated by patient"
    },
    "details": null
  },
  {
    "status": "unchanged",
    "dataset": {
      "id": "5a4b469a855d783af4f5f8db",
      "name": "AGG_AE_M",
      "label": "3.2- ADVERSE EVENTS MEDICATION ERROR- Aggregated by patient"
    },
    "details": null
  }]

想要得到的格式如下:

Capture.JPG
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2018-1-10 12:42:49
我已经写过一段code,但是只能解析一半数据:

import collections
import pandas as pd
import json

def flatten(d, parent_key='', sep='_'):
    items = []
    for k, v in d.items():
        new_key = parent_key + sep + k if parent_key else k

        is_lst = True if isinstance(v, list) else False

        if isinstance(v, collections.MutableMapping) or is_lst:
            if is_lst:
                items.extend(flatten(v[0], new_key, sep=sep).items())
            else:
                items.extend(flatten(v, new_key, sep=sep).items())
        else:
            items.append((new_key, v))
    return dict(items)

with open("reuse.txt") as f:
    dic = json.load(f)


df_ = [flatten(i) for i in dic]
df =pd.DataFrame(df_)

df.to_csv('out.csv', index=False)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群