如何使用Python json.dump以预期格式获取JSON数据

htrmnn0y  于 2022-11-21  发布在  Python
关注(0)|答案(1)|浏览(125)

我从excel文件中读取多个工作表,然后合并到一个JSON文件中。
示例数据:

df1

    Metric                      Value
0   salesamount                 9.0
1   salespercentage             80.0
2   salesdays                   56.0
3   salesconversionpercentage   0.3

df2

    Metric      Value
0   FromBudget  4K
1   ToBudget    5K

df3

    Metric                  Value
0   Objective               Customer Engagement
1   ExpectedOutcomesales    0.2
2   ExpectedOutcomeweeks    8 weeks

然后使用以下公式将它们转换为dictionary

s = dict(zip(df1.iloc[:,0], df1.iloc[:,1]))
eb = dict(zip(df2.iloc[:,0], df2.iloc[:,1]))
eo = dict(zip(df3.iloc[:,0], df3.iloc[:,1]))

然后,我将上面的items存储为密钥ExpectedPlanPerformance,使用:

mydct = {
        'ExpectedPlanPerformance' : 
                                    {
                                     'EstimatedBudget' : eb,
                                     'Sales' : s,
                                     'ExpectedOutcome' : eo
                                    }
        }
mydct

{'ExpectedPlanPerformance': {'EstimatedBudget': {'FromBudget': '4K',
   'ToBudget': '5K'},
  'Sales': ({'salesamount': '9.0',
    'salespercentage': '80.0',
    'salesdays': '56.0',
    'salesconversionpercentage': '0.3'},),
  'ExpectedOutcome': {'Objective': 'Customer Engagement',
   'ExpectedOutcomesales': 0.2,
   'ExpectedOutcomeweeks': '8 weeks'}}}

我将此dictionary写入JSON,使用:
如果您有一个文件,那么您就可以使用这个文件来创建一个文件。
我附加到的JSON文件已经包含其他元素。这些元素实际上是dataframes,使用以下命令转换为JSON

json.loads(df.to_json(orient = 'records'))

一旦这样的dataframes被转换为JSON,它们就被存储在如上所述的字典中,并使用相同的json.dump写入文件。
但文件中的输出格式如下:

{
    "ExpectedPlanPerformance": {
        "EstimatedBudget": "{\"FromBudget\": \"4K\", \"ToBudget\": \"5K\"}",
        "Sales": "{\"salesamount\": \"9.0\", \"salespercentage\": \"80.0\", \"salesdays\": \"56.0\", \"salesconversionpercentage\": \"0.3\"}",
        "ExpectedOutcome": "{\"Objective\": \"Customer Engagement\", \"ExpectedOutcomesales\": \"20%\", \"ExpectedOutcomeweeks\": \"8 weeks\"}"
    }

而其他一些元素如下:

"TotalYield": [
    "225K"
],
"TotalYieldText": [
    "Lorem ipsum door sit amet"
],

有人可以请让我知道如何解决这个问题,预期输出如下:

"ExpectedPlanPerformance": [{
    "ExpectedOutcome": {
        "Objective": "Customer Engagement",
        "ExpectedOutcomesales": "20%",
        "ExpectedOutcomeweeks": "8 weeks"
    },
    "Sales": {
        "salesamount": "9 ",
        "salespercentage": "80",
        "salesdays": "56",
        "salesconversionpercentage": "0.3"
    },
    "EstimatedBudget": {
        "FromBudget": "4K",
        "ToBudget": "5K"
    }
}],
6jygbczu

6jygbczu1#

请尝试:

out = {
    "ExpectedPlanPerformance": [
        {
            "ExpectedOutcome": dict(zip(df3.Metric, df3.Value)),
            "Sales": dict(zip(df1.Metric, df1.Value)),
            "EstimatedBudget": dict(zip(df2.Metric, df2.Value)),
        }
    ]
}

print(out)

印刷品:

{
    "ExpectedPlanPerformance": [
        {
            "ExpectedOutcome": {
                "Objective": "Customer Engagement",
                "ExpectedOutcomesales": "0.2",
                "ExpectedOutcomeweeks": "8 weeks",
            },
            "Sales": {
                "salesamount": 9.0,
                "salespercentage": 80.0,
                "salesdays": 56.0,
                "salesconversionpercentage": 0.3,
            },
            "EstimatedBudget": {"FromBudget": "4K", "ToBudget": "5K"},
        }
    ]
}

要将out保存到文件,请执行以下操作:

import json

with open("your_file.json", "w") as f_in:
    json.dump(out, f_in, indent=4)

相关问题