From a936a822af88aacd931bdd893772a489d5b23936 Mon Sep 17 00:00:00 2001 From: Alice BRENON <alice.brenon@ens-lyon.fr> Date: Wed, 22 Nov 2023 10:40:51 +0100 Subject: [PATCH] Add write capacities to the JSONL module --- scripts/ML/JSONL.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/scripts/ML/JSONL.py b/scripts/ML/JSONL.py index d191ee5..07b2aaf 100644 --- a/scripts/ML/JSONL.py +++ b/scripts/ML/JSONL.py @@ -1,6 +1,22 @@ import json def load(file_path): - with open(file_path, 'r') as input_file: - for line in input_file.readlines(): + if type(file_path) == str: + with open(file_path, 'r') as input_file: + for line in input_file.readlines(): + yield json.loads(line) + else: + for line in file_path.readlines(): yield json.loads(line) + +def save(file_path, objects): + if type(file_path) == str: + with open(file_path, 'w') as output_file: + saveObjects(output_file, objects) + else: + saveObjects(file_path, objects) + +def saveObjects(output_file, objects): + for obj in objects: + json.dump(obj, output_file) + print(file=output_file) -- GitLab