当前位置: 首页 > 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)是一种编程范式,它将数据和处理数据的方法封装在对象中。面向对象的主要概念包括: 对象:实例化的数据和方法的集合。类:对象的蓝图或模板。封装:隐藏对象的内部…...

uniapp 对接腾讯云IM群组成员管理(增删改查)

UniApp 实战:腾讯云IM群组成员管理(增删改查) 一、前言 在社交类App开发中,群组成员管理是核心功能之一。本文将基于UniApp框架,结合腾讯云IM SDK,详细讲解如何实现群组成员的增删改查全流程。 权限校验…...

VB.net复制Ntag213卡写入UID

本示例使用的发卡器:https://item.taobao.com/item.htm?ftt&id615391857885 一、读取旧Ntag卡的UID和数据 Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click轻松读卡技术支持:网站:Dim i, j As IntegerDim cardidhex, …...

JavaScript 中的 ES|QL:利用 Apache Arrow 工具

作者:来自 Elastic Jeffrey Rengifo 学习如何将 ES|QL 与 JavaScript 的 Apache Arrow 客户端工具一起使用。 想获得 Elastic 认证吗?了解下一期 Elasticsearch Engineer 培训的时间吧! Elasticsearch 拥有众多新功能,助你为自己…...

Opencv中的addweighted函数

一.addweighted函数作用 addweighted()是OpenCV库中用于图像处理的函数,主要功能是将两个输入图像(尺寸和类型相同)按照指定的权重进行加权叠加(图像融合),并添加一个标量值&#x…...

vue3 字体颜色设置的多种方式

在Vue 3中设置字体颜色可以通过多种方式实现&#xff0c;这取决于你是想在组件内部直接设置&#xff0c;还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法&#xff1a; 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...

前端开发面试题总结-JavaScript篇(一)

文章目录 JavaScript高频问答一、作用域与闭包1.什么是闭包&#xff08;Closure&#xff09;&#xff1f;闭包有什么应用场景和潜在问题&#xff1f;2.解释 JavaScript 的作用域链&#xff08;Scope Chain&#xff09; 二、原型与继承3.原型链是什么&#xff1f;如何实现继承&a…...

第 86 场周赛:矩阵中的幻方、钥匙和房间、将数组拆分成斐波那契序列、猜猜这个单词

Q1、[中等] 矩阵中的幻方 1、题目描述 3 x 3 的幻方是一个填充有 从 1 到 9 的不同数字的 3 x 3 矩阵&#xff0c;其中每行&#xff0c;每列以及两条对角线上的各数之和都相等。 给定一个由整数组成的row x col 的 grid&#xff0c;其中有多少个 3 3 的 “幻方” 子矩阵&am…...

在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?

uni-app 中 Web-view 与 Vue 页面的通讯机制详解 一、Web-view 简介 Web-view 是 uni-app 提供的一个重要组件&#xff0c;用于在原生应用中加载 HTML 页面&#xff1a; 支持加载本地 HTML 文件支持加载远程 HTML 页面实现 Web 与原生的双向通讯可用于嵌入第三方网页或 H5 应…...

Angular微前端架构:Module Federation + ngx-build-plus (Webpack)

以下是一个完整的 Angular 微前端示例&#xff0c;其中使用的是 Module Federation 和 npx-build-plus 实现了主应用&#xff08;Shell&#xff09;与子应用&#xff08;Remote&#xff09;的集成。 &#x1f6e0;️ 项目结构 angular-mf/ ├── shell-app/ # 主应用&…...

无人机侦测与反制技术的进展与应用

国家电网无人机侦测与反制技术的进展与应用 引言 随着无人机&#xff08;无人驾驶飞行器&#xff0c;UAV&#xff09;技术的快速发展&#xff0c;其在商业、娱乐和军事领域的广泛应用带来了新的安全挑战。特别是对于关键基础设施如电力系统&#xff0c;无人机的“黑飞”&…...