python - 如何操作给定 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"
}
]
解决方案
推荐阅读
- python - 如何使用 Button 将插入 GUI 的字符串值转换为其他函数可以使用的变量?
- scala - Scala 隐含在参数中
- oracle - 通过迭代未在光标中获得预期输出
- python - 计算R中圆的半径的倒数
- c# - CefSharp WinForms - 页面加载时闪烁
- android - Android Custom EditText 设置自定义默认样式(如果没有提供)
- python - 适用于 OSX 和 raspbian 的蛋白质数据库的 Python 脚本在 Ubuntu 中不起作用
- javascript - 在 Typescript 上反应重载 - CombineReducer
- php - Laravel 8 ErrorException“stream_get_meta_data() 期望参数 1 是资源,bool 给定”
- azure - 在 Azure SQL 数据仓库(突触)中指定 datetime2 格式