首页 > 解决方案 > 如何操作给定 json 输入文件中的数据并将其保存为所需格式

问题描述

我有一个文件input.txt,它实际上是嵌入在 JSON 结构中的逗号分隔字符串列表,其中行内的数据由管道 ( |) 分隔。

请注意last operation列如何在所需的输出中拆分为 LastOperation 和 OperationTime。

在这种情况下,使用两个空格作为字段分隔符/定界符。?

输入 json 文件为:

'{
    "info": {
        "status": "succ",
        "type": "command"
    },
    "data": {
        "list": [
            "CommQ data set name: test.ttq1.tt2.tt3",
            "               size: 19169280 bytes",
            "               used: 537320 bytes",
            "Destination name|status|     last operation     | sent |queued|address",
            "testifLAPPXDgx01    |active|Send OK  14.02.22 07 SEP|    26|     0|testifLAPPXDgx01..test.server ",
            "testifLAPPXDgx02    |active|Send OK  14.02.22 07 SEP|    12|     0|testifLAPPXDgx02..test.server ",
            "testifLAPPXDgx30    |active|Send OK  14.01.49 07 SEP|     7|     0|testifLAPPXDgx30..test.server ",
            "testifLAPPXDgx40    |active|Send OK  14.01.49 07 SEP|     7|     0|testifLAPPXDgx40..test.server ",

            "1105 senders, 0 quiesced"
        ]
    }
}'

需要输出:

[
    {
        "Destination": "testifLAPPXDgx01",
        "Status": "active",
        "LastOperation": "SEND OK",
        "OperationTime": "14.01.49 07 SEP",
        "Sent": 7,
        "Queued": 0,
        "Address": "testifLAPPXDgx01..test.server"
    },
    {
       "Destination": "testifLAPPXDgx02",
        "Status": "active",
        "LastOperation": "SEND OK",
        "OperationTime": "14.02.22 07 SEP",
        "Sent": 26,
        "Queued": 0,
        "Address": "testifLAPPXDgx02..test.server"
    }
]

标签: pythonjson

解决方案


推荐阅读