Created
July 11, 2024 21:24
-
-
Save manisnesan/e146aa0f0831a6e0581afcba6978d717 to your computer and use it in GitHub Desktop.
Read and Write jsonline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install ujson | |
from fastcore.utils import dumps | |
def write_jsonl(cases: list, filename: str)->None: | |
''' Write a list of dictionaries to a jsonl file. This uses fastcore.utils `dumps` to serialize the dictionary to a string with `ujson` module. | |
Install `ujson` for faster serialization.''' | |
with open(filename, 'w') as file: | |
for item in cases: file.write(dumps(item) + '\n') | |
def read_jsonl(fname)->list: | |
''' Read a jsonl file into a list of dictionaries. This uses `ujson` module for faster deserialization. Install `ujson` for faster deserialization.''' | |
import ujson | |
with open(fname, 'r') as file: return [ujson.loads(line) for line in file] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment