【问题标题】:Python Need to replace curly bracket with (curly bracket and comma)Python需要用(大括号和逗号)替换大括号
【发布时间】:2020-05-26 02:25:43
【问题描述】:

我有一个包含数十万行的 JSON 响应,并且全部从字典 python 解析并使用 json.load 转换为 JSON

现在收到无效响应的 JSON 响应,因此解决方法如下

回复

{
    "ComplianceType": "",
    "Details": {
        "InstalledTime": "",
        "PatchBaselineId": "",
        "PatchState": ""
    },
    "ExecutionSummary": {
        "ExecutionId": "",
        "ExecutionTime": "",
        "ExecutionType": ""
    },
    "Id": "",
    "ResourceId": "",
    "ResourceType": "",
    "Severity": "",
    "Status": "",
    "Title": ""
}
{
    "ComplianceType": "",
    "Details": {
        "InstalledTime": "",
        "PatchBaselineId": "",
        "PatchState": ""
    },
    "ExecutionSummary": {
        "ExecutionId": "",
        "ExecutionTime": "",
        "ExecutionType": ""
    },
    "Id": "",
    "ResourceId": "",
    "ResourceType": "",
    "Severity": "",
    "Status": "",
    "Title": ""
}

解决方法使其有效

[{
    "ComplianceType": "",
    "Details": {
        "InstalledTime": "",
        "PatchBaselineId": "",
        "PatchState": ""
    },
    "ExecutionSummary": {
        "ExecutionId": "",
        "ExecutionTime": "",
        "ExecutionType": ""
    },
    "Id": "",
    "ResourceId": "",
    "ResourceType": "",
    "Severity": "",
    "Status": "",
    "Title": ""
},
{
    "ComplianceType": "",
    "Details": {
        "InstalledTime": "",
        "PatchBaselineId": "",
        "PatchState": ""
    },
    "ExecutionSummary": {
        "ExecutionId": "",
        "ExecutionTime": "",
        "ExecutionType": ""
    },
    "Id": "",
    "ResourceId": "",
    "ResourceType": "",
    "Severity": "",
    "Status": "",
    "Title": ""
}
]

这些我需要通过程序来完成,当我尝试使用 sed shell --> #sed -i 's#}#},#' test.json

[{
    "ComplianceType": "",
    "Details": {
        "InstalledTime": "",
        "PatchBaselineId": "",
        "PatchState": ""
    },,
    "ExecutionSummary": {
        "ExecutionId": "",
        "ExecutionTime": "",
        "ExecutionType": ""
    },,
    "Id": "",
    "ResourceId": "",
    "ResourceType": "",
    "Severity": "",
    "Status": "",
    "Title": ""
},
{
    "ComplianceType": "",
    "Details": {
        "InstalledTime": "",
        "PatchBaselineId": "",
        "PatchState": ""
    },,
    "ExecutionSummary": {
        "ExecutionId": "",
        "ExecutionTime": "",
        "ExecutionType": ""
    },,
    "Id": "",
    "ResourceId": "",
    "ResourceType": "",
    "Severity": "",
    "Status": "",
    "Title": ""
},
]

请提供适用于 shell 或 python 的解决方法

【问题讨论】:

    标签: python json shell boto3


    【解决方案1】:

    根据您的示例输入,这可能适合您:

    $ cat input.json \
        | grep -v -e '^[[:space:]]*$' \
        | sed -e '1s/^/[/'   \
              -e '$s/$/]/'   \
              -e 's/^}$/},/'
    
    • grep 消除任何空行(如果存在)。这很重要,因为下面的sed 需要对最后一个非空行进行操作。
    • 第一个sed 表达式将[ 添加到第一行的开头
    • 第二个sed 表达式将] 添加到最后一行的末尾
    • 最后的sed 表达式在单独一行的任何} 之后添加一个逗号

    通过在第三个之前应用第二个sed,最后一个} 变为}],因此} 不再单独在一行中。

    【讨论】:

    • 谢谢!这就像一个魅力。我能知道我在哪里可以学习这些资源吗?