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