[英]Parse JSON file with Python
I trying to import a nested JSON file. 我试图导入嵌套的JSON文件。 I am using Python 3.0 The JSON file looks like this
我正在使用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"
}
}
]
}
]
I am using this code: 我正在使用此代码:
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'])
this code works fine but field transaction is not expanded 该代码可以正常工作,但不扩展现场交易
In order to expand transactions I have tried this: 为了扩展交易,我尝试了以下方法:
json_normalize(data_python,['funds','transactions'])
The information from transactions is expanded but I loose the other information Besides that, the field amount looks like this: 来自交易的信息得到了扩展,但我松开了其他信息,除此之外,字段金额如下所示:
{'amount': '1.00', 'currency': ''} {'amount':'1.00','currency':''}
and I am not able to get it into separate fields 我无法将其放入单独的字段
My question is how can I combine all the information into a single dataframe? 我的问题是如何将所有信息合并到一个数据框中?
尝试使用d['funds'][0]['transactions']
,其中d是字典的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.