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

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目:3442. 奇偶频次间的最大差值 I 思路 :哈希,时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况,哈希表这里用数组即可实现。 C版本: class Solution { public:int maxDifference(string s) {int a[26]…...

内存分配函数malloc kmalloc vmalloc

内存分配函数malloc kmalloc vmalloc malloc实现步骤: 1)请求大小调整:首先,malloc 需要调整用户请求的大小,以适应内部数据结构(例如,可能需要存储额外的元数据)。通常,这包括对齐调整,确保分配的内存地址满足特定硬件要求(如对齐到8字节或16字节边界)。 2)空闲…...

Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误

HTTP 状态码 406 (Not Acceptable) 和 500 (Internal Server Error) 是两类完全不同的错误,它们的含义、原因和解决方法都有显著区别。以下是详细对比: 1. HTTP 406 (Not Acceptable) 含义: 客户端请求的内容类型与服务器支持的内容类型不匹…...

Zustand 状态管理库:极简而强大的解决方案

Zustand 是一个轻量级、快速和可扩展的状态管理库,特别适合 React 应用。它以简洁的 API 和高效的性能解决了 Redux 等状态管理方案中的繁琐问题。 核心优势对比 基本使用指南 1. 创建 Store // store.js import create from zustandconst useStore create((set)…...

Frozen-Flask :将 Flask 应用“冻结”为静态文件

Frozen-Flask 是一个用于将 Flask 应用“冻结”为静态文件的 Python 扩展。它的核心用途是:将一个 Flask Web 应用生成成纯静态 HTML 文件,从而可以部署到静态网站托管服务上,如 GitHub Pages、Netlify 或任何支持静态文件的网站服务器。 &am…...

OkHttp 中实现断点续传 demo

在 OkHttp 中实现断点续传主要通过以下步骤完成,核心是利用 HTTP 协议的 Range 请求头指定下载范围: 实现原理 Range 请求头:向服务器请求文件的特定字节范围(如 Range: bytes1024-) 本地文件记录:保存已…...

Nginx server_name 配置说明

Nginx 是一个高性能的反向代理和负载均衡服务器,其核心配置之一是 server 块中的 server_name 指令。server_name 决定了 Nginx 如何根据客户端请求的 Host 头匹配对应的虚拟主机(Virtual Host)。 1. 简介 Nginx 使用 server_name 指令来确定…...

从零实现STL哈希容器:unordered_map/unordered_set封装详解

本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...

大模型多显卡多服务器并行计算方法与实践指南

一、分布式训练概述 大规模语言模型的训练通常需要分布式计算技术,以解决单机资源不足的问题。分布式训练主要分为两种模式: 数据并行:将数据分片到不同设备,每个设备拥有完整的模型副本 模型并行:将模型分割到不同设备,每个设备处理部分模型计算 现代大模型训练通常结合…...

CMake 从 GitHub 下载第三方库并使用

有时我们希望直接使用 GitHub 上的开源库,而不想手动下载、编译和安装。 可以利用 CMake 提供的 FetchContent 模块来实现自动下载、构建和链接第三方库。 FetchContent 命令官方文档✅ 示例代码 我们将以 fmt 这个流行的格式化库为例,演示如何: 使用 FetchContent 从 GitH…...