当前位置: 首页 > 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---------------
"""

http://www.lryc.cn/news/515962.html

相关文章:

  • 什么是网络安全攻防演练,即红蓝对抗?
  • 数据挖掘——决策树分类
  • Pytorch单、多GPU和CPU训练模型保存和加载
  • Karate 介绍与快速示例(API测试自动化、模拟、性能测试与UI自动化工具)
  • Pytest 高级用法:间接参数化
  • 第07章 存储管理(一)
  • Go语言的 的设计模式(Design Patterns)核心知识
  • js函数预览图片:支持鼠标和手势拖拽缩放
  • 用QT实现 端口扫描工具1
  • 设计模式 结构型 适配器模式(Adapter Pattern)与 常见技术框架应用 解析
  • vue 项目集成 electron 和 electron 打包及环境配置
  • vscode如何离线安装插件
  • 计算机网络常见面试题及解答
  • 举例说明AI模型怎么聚类,最后神经网络怎么保存
  • HarmonyOS NEXT应用开发实战(一):边学边玩,从零开发一款影视APP
  • STM32G0B1 can Error_Handler 解决方法
  • 使用 `llama_index` 构建智能问答系统:多种文档切片方法的评估
  • 【大模型】7 天 AI 大模型学习
  • 软件工程大复习之(四)——面向对象与UML
  • 【Linux】shell命令
  • ValuesRAG:以检索增强情境学习强化文化对齐
  • 【机器学习篇】交通革命:机器学习如何引领未来的道路创新
  • DeepSeek-V3 通俗详解:从诞生到优势,以及与 GPT-4o 的对比
  • 把vue项目或者vue组件发布成npm包或者打包成lib库文件本地使用
  • 【STC库函数】Compare比较器的使用
  • 单片机-独立按键矩阵按键实验
  • 若要把普通表转成分区表,就需要先新建分区表,然后把普通表中的数据导入新建分区表。 具体怎么导入?
  • XXX公司面试真题
  • 第一节:电路连接【51单片机+A4988+步进电机教程】
  • 机器学习算法深度解析:以支持向量机(SVM)为例的实践应用