使用python和Json到blender

2024-09-30 00:30:07 发布

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

我有一个项目,我必须把json文件转换成CSV文件。在

Json示例:

{

  "P_Portfolio Group": {

  "depth": 1,

  "dataType": "PortfolioOverview",

  "levelId": "P_Portfolio Group",

  "path": [

     {

        "label": "Portfolio Group",

        "levelId": "P_Portfolio Group"

     }

  ],

  "label": "Portfolio Group",

  "header": [

     {

        "id": "Label",

        "label": "Security name",

        "type": "text",

        "contentType": "text"

     },

     {

        "id": "SecurityValue",

        "label": "MioCHF",

        "type": "text",

        "contentType": "number"

     },

     {

        "id": "SecurityValuePct",

        "label": "%",

        "type": "text",

        "contentType": "pct"

     }

  ],

  "data": [

     {

        "dataValues": [

           {

              "value": "Client1",

              "type": "text"

           },

           {

              "value": 2068.73,

              "type": "number"

           },

           {

              "value": 14.0584,

              "type": "pct"

           }

        ]

     },

     {

        "dataValues": [

           {

              "value": "Client2",

              "type": "text"

           },

           {

              "value": 1511.9,

              "type": "number"

           },

           {

              "value": 10.2744,

              "type": "pct"

           }

        ]

     },

     {

        "dataValues": [

           {

              "value": "Client3",

              "type": "text"

           },

           {

              "value": 1354.74,

              "type": "number"

           },

           {

              "value": 9.2064,

              "type": "pct"

           }

        ]

     },

     {

        "dataValues": [

           {

              "value": "Client4",

              "type": "text"

           },

           {

              "value": 1225.78,

              "type": "number"

           },

           {

              "value": 8.33,

              "type": "pct"

           }

        ]

     }


  ],

  "summary": [

     {

        "value": "Total",

        "type": "text"

     },

     {

        "value": 11954.07,

        "type": "number"

     },

     {

        "value": 81.236,

        "type": "pct"

     }

  ]

}

}

我想得到一些东西,比如:

^{pr2}$

你能给我个提示吗。在

import csv
import json

with open("C:\Users\SVC\Desktop\test.json") as file:
    x = json.load(file)

f = csv.writer(open("C:\Users\SVC\Desktop\test.csv", "wb+"))

for x in x:
    f.writerow(x["P_Portfolio Group"]["data"]["dataValues"]["value"])

但它不起作用。在

你能给我个提示吗。在


Tags: 文件csvtextidjsonnumbervaluetype
2条回答

使用熊猫图书馆:

import pandas as pd
data = pd.read_csv("C:\Users\SVC\Desktop\test.json")
data.to_csv('test.csv')

完成

import csv
import json

with open('C:\Users\SVC\Desktop\test.json') as json_file:
    portfolio_group = json.load(json_file)

with open('C:\Users\SVC\Desktop\test.csv', 'w') as csv_file:
    csv_obj = csv.writer(csv_file)
    for data in portfolio_group['P_Portfolio Group']['data']:
        csv_obj.writerow([d['value'] for d in data['dataValues']])

这将产生以下C:\Users\SVC\Desktop\test.csv内容:

^{pr2}$

相关问题 更多 >

    热门问题