From 3b5d6ae8fc96459549f00b96aa100079b918178c Mon Sep 17 00:00:00 2001 From: smilelight Date: Thu, 22 Aug 2024 15:27:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=BB=E5=8F=96json?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lightutils/io/__init__.py | 4 ++-- lightutils/io/json_file.py | 12 ++++++++++++ requirements.txt | 3 ++- version.txt | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lightutils/io/__init__.py b/lightutils/io/__init__.py index 0135e17..3caf8b8 100644 --- a/lightutils/io/__init__.py +++ b/lightutils/io/__init__.py @@ -1,4 +1,4 @@ -from .json_file import read_json_line, write_json_line +from .json_file import read_json_line, write_json_line, get_json_list, get_std_json_list from .notification import send_email_notification from .yaml_file import load_yaml -__all__ = ['read_json_line', 'send_email_notification', 'write_json_line', 'load_yaml'] +__all__ = ['read_json_line', 'send_email_notification', 'write_json_line', 'load_yaml', 'get_json_list', 'get_std_json_list'] diff --git a/lightutils/io/json_file.py b/lightutils/io/json_file.py index 45c4b99..55247c4 100644 --- a/lightutils/io/json_file.py +++ b/lightutils/io/json_file.py @@ -1,6 +1,8 @@ import json import os +from tqdm import tqdm + def read_json_line(filename: str, error_file: str = None): """ @@ -33,3 +35,13 @@ def write_json_line(file_obj, json_obj): """ file_obj.write(json.dumps(json_obj, ensure_ascii=False)) file_obj.write('\n') + + +def get_json_list(path): + with open(path, encoding="utf8") as f: + for line in tqdm(f): + yield json.loads(line.strip()) + +def get_std_json_list(): + for line in tqdm(sys.stdin): + yield json.loads(line.strip()) diff --git a/requirements.txt b/requirements.txt index 8e4ba81..0d6ff53 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ requests yagmail -PyYAML \ No newline at end of file +PyYAML +tqdm \ No newline at end of file diff --git a/version.txt b/version.txt index 44a7df2..44905e7 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.1.17 \ No newline at end of file +0.1.18 \ No newline at end of file