python处理类似json的文件

前言

有些文件长得像json的键值对格式,但又不完全是。有时需要提取出其中某些值,可以先手动处理成json文件,然后用python的json模块。

示例1:每行键值对

提取其中的caseId的值

{"isself":"1","reason":"","caseId":"123456","callResult":"1","attitude":"0"}
{"callFailReason":"不确定","caseId":"234567","callResult":"0"}
{"callFailReason":"暂时无法连接","caseId":"345678","callResult":"0"}
{"callFailReason":"暂时无法连接","caseId":"456789","callResult":"0"}
{"callFailReason":"超时","caseId":"567891","callResult":"0"}
{"callFailReason":"暂时无法连接","caseId":"678912","callResult":"0"}
...
{"callFailReason":"暂时无法连接","caseId":"789123","callResult":"0"}
  1. 使用sed在行尾批量插入,
sed -i 's/$/,/g' ./test.txt
  1. 手动在文件开始的地方和文件末尾添加中括号,删除最后一行的逗号,接着后缀名改为.json。最终处理示例:
[{"isself":"1","reason":"","caseId":"123456","callResult":"1","attitude":"0"},
{"callFailReason":"不确定","caseId":"234567","callResult":"0"},
...
{"callFailReason":"暂时无法接通","caseId":"345678","callResult":"0"}]
  1. 编辑python脚本:process_jsonlike.py
import json

with open("test.json","r",encoding="utf-8") as f1:
    rst = json.load(f1)
    
with open("rst.txt", "a",encoding="utf-8") as f2:
    for i in rst:
        caseid = i["caseId"]
        f2.write(f"{caseid}\n")
  1. 查看结果文件:rst.txt
head rst.txt

未完待续

占位

posted @ 2022-01-20 11:01  花酒锄作田  阅读(139)  评论(0编辑  收藏  举报