python中ijson的用法,解决大json文件中提取数据
·
import ijson
def extract_value(json_file_path, target_key):
with open(json_file_path, 'r', encoding='utf-8') as file:
# 使用 ijson.items 方法进行流式解析
items = ijson.items(file, 'a')
# 提取值
for item in items:
# 如果目标键存在于当前项中,返回相应的值
if target_key in item:
print(target_key)
yield target_key
# 示例用法
json_file_path = 'test.json' # 里面包含 {"a": "a"}
target_key = 'a'
for value in extract_value(json_file_path, target_key):
print(value)
更多推荐
所有评论(0)