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---------------
"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值