当前位置: 首页 > news >正文

python ijson 用法教程

 ijson · PyPI

Python ijson处理大型JSON文件 - 秀尊云 

 Python解析JSON大文件 | Leetao's Blog

https://stackoverflow.com/questions/2400643/is-there-a-memory-efficient-and-fast-way-to-load-big-json-files/58148422#58148422 Python中读写(解析)JSON文件的深入探究-阿里云开发者社区

 

import ijson"""
{"earth": {"europe": [{"name": "Paris", "type": "city", "info": {"a": "b"}},{"name": "Thames", "type": "river", "info": {"a": [1, 2, 3]}}],"america": [{"name": "Texas", "type": "state", "info": {"a":"c"}}]}
}
"""
def parse(filename):with open(filename) as input_file:for prefix, event, value in ijson.parse(input_file):print("prefix ", prefix, end=" ")print("event ", event, end=" ")print("value ", value)print("***********************")def json_parse_earth(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "earth"):# for item in  ijson.items(file, "earth"):print(item)print("earth---------------")def json_parse_europe(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "earth.europe"):# for item in  ijson.items(file, "earth.europe"):print(item)print("europe---------------")def json_parse_item(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "earth.europe.item"):# for item in  ijson.items(file, "earth.europe.item"):print(item)print("item---------------")def json_parse_name(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "earth.europe.item.name"):# for item in  ijson.items(file, "earth.europe.item.name"):print(item)print("name---------------")def json_parse_type(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "earth.europe.item.type"):# for item in  ijson.items(file, "earth.europe.item.type"):print(item)print("type---------------")def json_parse_info(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "earth.europe.item.info"):# for item in  ijson.items(file, "earth.europe.item.info"):print(item)print("info---------------")def json_parse_a(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "earth.europe.item.info.a"):# for item in  ijson.items(file, "earth.europe.item.info.a"):print(item)print("a---------------")parse("./json_file")
"""
prefix   (空)
prefix  earth
prefix  earth.europe
prefix  earth.europe.item
prefix  earth.europe.item.name
prefix  earth.europe.item.type
prefix  earth.europe.item.info
prefix  earth.europe.item.info.a
prefix  earth.america
prefix  earth.america.item
prefix  earth.america.item.name
prefix  earth.america.item.type
prefix  earth.america.item.info
prefix  earth.america.item.info.a
"""
json_parse_earth("./json_file")
"""
{'europe': [{'name': 'Paris', 'type': 'city', 'info': {'a': 'b'}}, {'name': 'Thames', 'type': 'river', 'info': {'a': [1, 2, 3]}}], 'america': [{'name': 'Texas', 'type': 'state', 'info': {'a': 'c'}}]}
earth---------------
"""
json_parse_europe("./json_file")
"""
[{'name': 'Paris', 'type': 'city', 'info': {'a': 'b'}}, {'name': 'Thames', 'type': 'river', 'info': {'a': [1, 2, 3]}}]
europe---------------
"""
json_parse_item("./json_file")
"""
{'name': 'Paris', 'type': 'city', 'info': {'a': 'b'}}
item---------------
{'name': 'Thames', 'type': 'river', 'info': {'a': [1, 2, 3]}}
item---------------
"""
json_parse_name("./json_file")
"""
Paris
name---------------
Thames
name---------------
"""
json_parse_type("./json_file")
"""
city
type---------------
river
type---------------
"""
json_parse_info("./json_file")
"""
{'a': 'b'}
info---------------
{'a': [1, 2, 3]}
info---------------
"""
json_parse_a("./json_file")
"""
b
a---------------
[1, 2, 3]
a---------------
"""

 

import ijson"""
[
{"name": "rantidine",  "drug": {"type": "tablet", "content_type": "solid"}},
{"name": "nicip",  "drug": {"type": "capsule", "content_type": "solid"}}
]
"""
def parse(filename):with open(filename) as input_file:for prefix, event, value in ijson.parse(input_file):print("prefix ", prefix, end=" ")print("event ", event, end=" ")print("value ", value)print("***********************")def json_parse_item(json_file):with open(json_file) as file:for item in  ijson.items(file, "item"):print(item)print("item---------------")def json_parse_name(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "item.name"):print(item)print("name---------------")def json_parse_drug(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "item.drug"):print(item)print("drug---------------")def json_parse_type(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "item.drug.type"):print(item)print("type---------------")parse("./json_file2")
"""
prefix   (空)
prefix  item
prefix  item.name
prefix  item.drug
prefix  item.drug.type
prefix  item.drug.content_type
"""
json_parse_item("./json_file2")
"""
{'name': 'rantidine', 'drug': {'type': 'tablet', 'content_type': 'solid'}}
item---------------
{'name': 'nicip', 'drug': {'type': 'capsule', 'content_type': 'solid'}}
item---------------
"""
json_parse_name("./json_file2")
"""
rantidine
name---------------
nicip
name---------------
"""
json_parse_drug("./json_file2")
"""
{'type': 'tablet', 'content_type': 'solid'}
drug---------------
{'type': 'capsule', 'content_type': 'solid'}
drug---------------
"""
json_parse_type("./json_file2")
"""
tablet
type---------------
capsule
type---------------
"""

 

import ijson"""
[{"earth": "diqiu","europe": null,"name":"","type": 0,"info": "1\n2"}
]
"""
def parse(filename):with open(filename) as input_file:for prefix, event, value in ijson.parse(input_file):print("prefix ", prefix, end=" ")print("event ", event, end=" ")print("value ", value)print("***********************")def json_parse_item(json_file):with open(json_file) as file:for item in  ijson.items(file, "item"):print(item)print("item---------------")def json_parse_info(json_file):with open(json_file) as file:paser = ijson.parse(file)for item in  ijson.items(paser, "item.info"):print(item)print("info---------------")parse("./json_file3")
"""
prefix   (空)
prefix  item
prefix  item.earth
prefix  item.europe
prefix  item.name
prefix  item.type
prefix  item.info
"""
json_parse_item("./json_file3")
"""
{'earth': 'diqiu', 'europe': None, 'name': '', 'type': 0, 'info': '1\n2'}
item---------------
"""
json_parse_info("./json_file3")
"""
1
2
info---------------
"""

相关文章:

python ijson 用法教程

ijson PyPI Python ijson处理大型JSON文件 - 秀尊云 Python解析JSON大文件 | Leetaos Blog https://stackoverflow.com/questions/2400643/is-there-a-memory-efficient-and-fast-way-to-load-big-json-files/58148422#58148422 Python中读写(解析)J…...

什么是网络安全攻防演练,即红蓝对抗?

定义与目的 定义:网络安全攻防演练是一种模拟真实网络攻击和防御场景的活动,通过组织专业的攻击队伍(红队)和防御队伍(蓝队)进行对抗,来检验和提升组织的网络安全防御能力、应急响应能力和安全运…...

数据挖掘——决策树分类

数据挖掘——决策树分类 决策树分类Hunt算法信息增益增益比率基尼指数连续数据总结 决策树分类 树状结构,可以很好的对数据进行分类; 决策树的根节点到叶节点的每一条路径构建一条规则;具有互斥且完备的特点,即每一个样本均被且…...

Pytorch单、多GPU和CPU训练模型保存和加载

Pytorch多GPU训练模型保存和加载 在多GPU训练中,模型通常被包装在torch.nn.DataParallel或torch.nn.parallel.DistributedDataParallel中,这会在模型的参数名前加上module前缀。因此,在保存模型时,需要使用model.module.state_di…...

Karate 介绍与快速示例(API测试自动化、模拟、性能测试与UI自动化工具)

Karate是一个将API测试自动化、模拟、性能测试甚至UI自动化结合到一个统一框架中的开源工具。 Karate使用Gherkin 的BDD语法,是语言中性的,即使是非程序员也很容易。断言和HTML报告是内置的,支持并行运行测试以提高速度Karate 是用Java语言编写, 可以在Java 项目项目中运行…...

Pytest 高级用法:间接参数化

文章目录 1. 引言2. 基础概念2.1 Fixture2.2 参数化 3. 代码实例3.1 基础设置3.2 测试用例示例示例 1:基础的间接参数化示例 2:通过 request 获取参数值示例 3:多参数组合测试示例 4:部分间接参数化 4. 最佳实践5. 总结参考资料 1…...

第07章 存储管理(一)

一、磁盘简介 1.1 名称称呼 磁盘/硬盘/disk是同一个东西,不同于内存的是容量比较大。 1.2 类型 机械:机械硬盘即是传统普通硬盘,主要由:盘片,磁头,盘片转轴及控制电机,磁头控制器&#xff0…...

Go语言的 的设计模式(Design Patterns)核心知识

Go语言的设计模式(Design Patterns)核心知识 Go语言(Golang)是一种静态类型、编译型的编程语言,自2009年由Google正式推出以来,因其高效的性能、卓越的并发能力以及简洁的语法受到广泛欢迎。在软件开发中&…...

js函数预览图片:支持鼠标和手势拖拽缩放

对之前的方式改进:原生js实现图片预览控件,支持丝滑拖拽,滚轮放缩,放缩聚焦_js图片预览-CSDN博客 /*** 图片预览函数,调用后自动预览图片* param {图片地址} imgurl*/ function openImagePreview(imgurl) {if (!imgurl…...

用QT实现 端口扫描工具1

安装在线QT,尽量是完整地自己进行安装,不然会少包 参考【保姆级图文教程】QT下载、安装、入门、配置VS Qt环境-CSDN博客 临时存储空间不够。 Windows系统通常会使用C盘来存储临时文件。 修改临时文件存储位置 打开系统属性: 右键点击“此电…...

设计模式 结构型 适配器模式(Adapter Pattern)与 常见技术框架应用 解析

适配器模式(Adapter Pattern)是一种结构型设计模式,它允许将一个类的接口转换成客户端所期望的另一个接口,从而使原本因接口不兼容而无法一起工作的类能够协同工作。这种设计模式在软件开发中非常有用,尤其是在需要集成…...

vue 项目集成 electron 和 electron 打包及环境配置

vue electron 开发桌面端应用 安装 electron npm i electron -D记得加上-D,electron 需添加到devDependencies,如果添加到dependencies后面运行可能会报错 根目录创建electron文件夹,在electron文件夹创建main.js(或者backgrou…...

vscode如何离线安装插件

在没有网络的时候,如果要安装插件,就会麻烦一些,需要通过离线安装的方式进行。下面记录如何在vscode离线安装插件。 一、下载离线插件 在一台能联网的电脑中,下载好离线插件,拷贝到无法联网的电脑上。等待安装。 vscode插件商店地址:https://marketplace.visualstudio.co…...

计算机网络常见面试题及解答

以下是计算机网络中常见的面试题及解答,按主题分类: --- ## **一、基础概念** ### **1. OSI 七层模型和 TCP/IP 模型的区别是什么?** **答:** - **OSI 七层模型:** - 应用层、表示层、会话层、传输层、网络层、数…...

举例说明AI模型怎么聚类,最后神经网络怎么保存

举例说明怎么聚类,最后神经网络怎么保存 目录 举例说明怎么聚类,最后神经网络怎么保存K - Means聚类算法实现神经元特征聚类划分成不同专家的原理和过程 特征提取: 首先,需要从神经元中提取有代表性的特征。例如,对于一个多层感知机(MLP)中的神经元,其权重向量可以作为特…...

HarmonyOS NEXT应用开发实战(一):边学边玩,从零开发一款影视APP

引言 学习一项技能,最好也最快的办法就是动手实战。通过自己给自己找项目练习,不仅能够激发兴趣,还能从开发实战中不断总结经验。这种学习方法是最为高效的。今天,我们将通过开发一款名为“爱影家”的影视APP,来学习H…...

STM32G0B1 can Error_Handler 解决方法

问题现象 MCU上电,发送0x13帧数据固定进入 Error_Handler 硬件介绍 MCU :STM32G0B1 can:NSI1042 tx 接TX RX 接RX 折腾了一下午,无解,问题依旧; 对比测试 STM32G431 手头有块G431 官方评估版CAN 模块; 同样的…...

使用 `llama_index` 构建智能问答系统:多种文档切片方法的评估

使用 llama_index 构建智能问答系统:多种文档切片方法的评估 代码优化与解析1. **代码结构优化**2. **日志管理**3. **环境变量管理**4. **模型初始化**5. **提示模板更新**6. **问答函数优化**7. **索引构建与查询引擎**8. **节点解析器测试** 总结 在现代自然语言…...

【大模型】7 天 AI 大模型学习

7 天 AI 大模型学习 Day 2 今天是 7 天AI 大模型学习的第二天 😄,今天我将会学习 Transformer 、Encoder-based and Decoder-Based LLMs 等 。如果有感兴趣的,就和我一起开始吧 ~ 课程链接 :2025年快速吃透AI大模型&am…...

软件工程大复习之(四)——面向对象与UML

4.1 面向对象概述 面向对象(OO)是一种编程范式,它将数据和处理数据的方法封装在对象中。面向对象的主要概念包括: 对象:实例化的数据和方法的集合。类:对象的蓝图或模板。封装:隐藏对象的内部…...

2026届学术党必备的降重复率网站推荐榜单

Ai论文网站排名(开题报告、文献综述、降aigc率、降重综合对比) TOP1. 千笔AI TOP2. aipasspaper TOP3. 清北论文 TOP4. 豆包 TOP5. kimi TOP6. deepseek 国内权威学术数据库知网,已正式开展AIGC检测服务,此服务依据深度学习…...

【紧急预警】AGI基础设施准备窗口仅剩18个月:SITS2026圆桌发布《企业AGI就绪度自评矩阵》(含6大维度22项硬指标)

第一章:SITS2026圆桌:AGI何时到来 2026奇点智能技术大会(https://ml-summit.org) 圆桌共识与分歧焦点 在SITS2026主会场举行的“AGI何时到来”圆桌论坛中,来自DeepMind、Anthropic、中科院自动化所及OpenAI前核心架构师的六位专家展开激烈交…...

飞书文档批量导出工具:企业知识库迁移的完整解决方案

飞书文档批量导出工具:企业知识库迁移的完整解决方案 【免费下载链接】feishu-doc-export 飞书文档导出服务 项目地址: https://gitcode.com/gh_mirrors/fe/feishu-doc-export 在数字化转型的浪潮中,企业知识管理面临着平台迁移的挑战。当您的团队…...

PyPTO Agent 实操:1天开发自定义融合算子

一、PyPTO Agent背景在 Agent 技术日益普及的当下,为了提升开发体验,我们推出了基于智能体平台 CANNBot 与高性能编程框架 PyPTO 的 CANNBot PyPTO Agent。通过将最佳实践固化为 7 个标准化 Skill,并由 4 个专业 Agent 进行协同调度&#xff…...

如何快速配置游戏自动化助手:面向新手的完整指南

如何快速配置游戏自动化助手:面向新手的完整指南 【免费下载链接】MaaAssistantArknights 《明日方舟》小助手,全日常一键长草!| A one-click tool for the daily tasks of Arknights, supporting all clients. 项目地址: https://gitcode.…...

打破平台壁垒:WorkshopDL如何让非Steam玩家也能畅享创意工坊模组

打破平台壁垒:WorkshopDL如何让非Steam玩家也能畅享创意工坊模组 【免费下载链接】WorkshopDL WorkshopDL - The Best Steam Workshop Downloader 项目地址: https://gitcode.com/gh_mirrors/wo/WorkshopDL 想象一下这个场景:你在GOG平台购买了一…...

AGI何时真正落地?基于17国算力增长曲线、神经符号融合进展与监管阈值的7维交叉验证分析

第一章:AGI何时真正落地?基于17国算力增长曲线、神经符号融合进展与监管阈值的7维交叉验证分析 2026奇点智能技术大会(https://ml-summit.org) 当前AGI落地时间预测分歧显著,主流模型仍受限于因果推理缺失、跨域泛化脆弱及可验证性不足三大瓶…...

终极指南:使用开源工具解决NVIDIA显卡显示器色彩失真问题

终极指南:使用开源工具解决NVIDIA显卡显示器色彩失真问题 【免费下载链接】novideo_srgb Calibrate monitors to sRGB or other color spaces on NVIDIA GPUs, based on EDID data or ICC profiles 项目地址: https://gitcode.com/gh_mirrors/no/novideo_srgb …...

别再只盯着VL817了!聊聊它的进阶版VL817S,以及如何用外部LDO搞定供电设计

VL817S进阶实战:外部LDO供电设计与硬件选型全解析 当USB Hub控制器选型遇上成本与性能的平衡难题,硬件工程师的抽屉里总少不了一颗VL817。但今天我们要聊的不是这位"老将",而是它的进阶版本——VL817S。这个看似简单的型号后缀变化…...

拆解对比:Holtek BS45F3833 vs 传统方案,为什么它能成为超声波雾化行业新标杆?

Holtek BS45F3833芯片深度解析:超声波雾化技术的革新与突破 在智能家居和健康设备领域,超声波雾化技术正经历着一场静默的革命。从加湿器到香薰机,从医疗雾化到工业加湿,这项技术的应用场景不断扩展,而驱动这些设备的核…...