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"}
- 使用
sed
在行尾批量插入,
sed -i 's/$/,/g' ./test.txt
- 手动在文件开始的地方和文件末尾添加中括号,删除最后一行的逗号,接着后缀名改为
.json
。最终处理示例:
[{"isself":"1","reason":"","caseId":"123456","callResult":"1","attitude":"0"},
{"callFailReason":"不确定","caseId":"234567","callResult":"0"},
...
{"callFailReason":"暂时无法接通","caseId":"345678","callResult":"0"}]
- 编辑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")
- 查看结果文件:
rst.txt
head rst.txt
未完待续
占位
本文来自博客园,作者:花酒锄作田,转载请注明原文链接:https://www.cnblogs.com/XY-Heruo/p/15825499.html