使用Python解析JSON文件

时间:2017-07-05 13:41:59

标签: python json pandas

我尝试导入嵌套的JSON文件。我使用的是Python 3.0 JSON文件看起来像这样

 "funds": [
    {
      "branch": "****",
      "controlDigits": "**",
      "accountNumber": "7605390244",
      "balance": {
        "amount": "71.1",
        "currency": "EUR"
      },
      "fundName": "Eurobits Funds 0",
      "webAlias": "Eurobits Funds 0",
      "performance": "4.41",
      "performanceDescription": "",
      "yield": {
        "amount": "0.0",
        "currency": "EUR"
      },
      "quantity": "10.00",
      "valueDate": "30/03/2017",
      "transactions": [
        {
          "operationType": "1",
          "operationDescription": "MOVILIZACION HACIA DENTRO",
          "operationDate": "30/03/2017",
          "fundName": "B EVOLUCION PRUDEN",
          "quantity": "-809.27",
          "unitPrice": "7.98",
          "operationAmount": {
            "amount": "-6457.97",
            "currency": "EUR"
          }
        }
      ]
    }
  ]

我正在使用此代码:

from pandas.io.json import json_normalize
data_json = open("prueba.json",mode='r', encoding="utf8").read().replace('\n', '').replace('\t', '')
data_python = json.loads(data_json)

json_normalize(data_python['funds'])

此代码工作正常但字段事务未扩展

为了扩展交易,我试过这个:

json_normalize(data_python,['funds','transactions'])

来自交易的信息已扩展,但我放弃了其他信息 除此之外,字段数量如下:

{'金额':' 1.00','货币':''}

我无法将其分成不同的字段

我的问题是如何将所有信息合并到一个数据框中?

1 个答案:

答案 0 :(得分:0)

请尝试使用d['funds'][0]['transactions'],其中d是字典的名称。