python · 读取多个json对象

python 专栏收录该内容
10 篇文章 0 订阅

当遇到像这样含有多个json对象的文件,读取需要用到jsonlines

1/ pip install jsonlines 安装jsonlines依赖

 

2/ 要读取的文件示例

文件位置:

文件内容:

{"type":"event","timestamp":"2018-03-25 09:19:50.999","event":"ButtonClicked","params":{"screen":"MainScreen","button":"SettingsButton"}}
{"type":"event","timestamp":"2018-03-25 09:19:51.061","event":"ScreenShown","params":{"name":"SettingsScreen"}}
{"type":"event","timestamp":"2018-03-25 09:19:53.580","event":"ButtonClicked","params":{"screen":"SettingsScreen","button":"MissionsButton"}}
{"type":"event","timestamp":"2018-03-25 09:19:53.615","event":"ScreenShown","params":{"name":"MissionsScreen"}}

 

3/ handle-json.py为处理json的文件

import jsonlines

# 遍历读取json对象
with open("./../datas/jsontest-01.json", "r+", encoding="utf8") as f:
    for item in jsonlines.Reader(f):
        print(item)

 

4/ 运行效果

  • 1
    点赞
  • 0
    评论
  • 7
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值