【问题标题】:How to convert nested string dictionary without quotes to dictionary in Python如何将不带引号的嵌套字符串字典转换为Python中的字典
【发布时间】:2021-11-21 16:07:03
【问题描述】:

我有下面没有引号的嵌套字符串字典,我想将其转换为 python 字典。

                { id: 0,
                  label: 'Data0',
                  axis: "left",
                  color: "#0000ff",
                  avg: "383.04347826086956",
                  last: "378.0",
                  min: "282.0",
                  max: "439.0" }
                ,
                { id: 1,
                  label: 'Data1',
                  axis: "left",
                  color: "#00ff00",
                  avg: "",
                  last: "",
                  min: "",
                  max: "" }

预期输出:

                { "id": 0,
                  "label": "Data0",
                  "axis": "left",
                  "color": "#0000ff",
                  "avg": 383.04347826086956,
                  "last": 378.0,
                  "min": 282.0,
                  "max": 439.0 }
                ,
                { "id: 1,
                  "label": "Data1",
                  "axis": "left",
                  "color": "#00ff00",
                  "avg": "",
                  "last": "",
                  "min": "",
                  "max": "" }

这样做的主要原因是从 API 响应中获取字符串形式的输出,其中包含我使用 split() 方法删除的许多其他内容。

【问题讨论】:

  • 这是从哪里来的?它作为 Python JSON 无效。
  • 此 API 是否使用已知格式?您可能会发现为此需要一个相当复杂的解析器。
  • 我认为如果你展示一个实际响应的例子——即在你开始篡改它之前,它会有所帮助。正如@tdelaney 所说,如果它不适合 Python,那么您可能需要开发一个自定义解析器。祝你好运
  • 我不确定您所说的“嵌套字符串字典”是什么意思。那只是一个python字符串吗?或者那些 python 字典使用来自名为“id”、“label”等的 python 变量的值......如果它是一个你试图解析的单个 python 字符串,请将所有这些都放在 python 三引号中,这样我们就知道了。
  • 请找到我得到的实际原始输出pastecode.io/s/x8uxaudc这里我想提取summaryData

标签: python json string dictionary


【解决方案1】:

试试:

js_data = """
/*
 * Pure Javascript, which calls the specified callback function, specified using the Jsonp parameter
 *
 * Callback function is passed all parameters necessary to render chart
 */

MP.ChartController.loaded('chartdiv',
{
    error: '',
    width: 1480,
    height: 308,
    summaryData: [

        {
            id: 0,
            label: 'Data0',
            axis: "left",
            color: "#0000ff",
            avg: "383.04347826086956",
            last: "378.0",
            min: "282.0",
            max: "439.0"
        },
        {
            id: 1,
            label: 'PQ Initiated',
            axis: "left",
            color: "#00ff00",
            avg: "",
            last: "",
            min: "",
            max: ""
        }
    ],
    graphType: 'chart',
    warnings: []
});
"""

import re
import json

# find `summaryData`
summary_data = re.search(r"summaryData: (\[.*?\]),", js_data, flags=re.S)

# add quotes("") around keys
summary_data = re.sub(r"(\S+):", r'"\1":', summary_data.group(1))

# replace ' to "
summary_data = summary_data.replace("'", '"')

# decode the string:
summary_data = json.loads(summary_data)

print(summary_data)

打印:

[
    {
        "id": 0,
        "label": "Data0",
        "axis": "left",
        "color": "#0000ff",
        "avg": "383.04347826086956",
        "last": "378.0",
        "min": "282.0",
        "max": "439.0",
    },
    {
        "id": 1,
        "label": "PQ Initiated",
        "axis": "left",
        "color": "#00ff00",
        "avg": "",
        "last": "",
        "min": "",
        "max": "",
    },
]

【讨论】:

  • 此解决方案取决于数据。如果一个值具有像12:20:00 这样的时间值,则它不起作用。但有时这就是数据黑客的意义所在。
猜你喜欢
  • 2022-07-20
  • 2019-03-07
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多