小编cod*_*bie的帖子

gzipped jsonlines文件在python中读写

虽然此代码读取和写入 jsonlines 文件。怎么压缩呢?我尝试直接使用gzip.open,但出现各种错误。

import json
    
def dump_jsonl(data, output_path, append=False):
    """
    Write list of objects to a JSON lines file.
    """
    mode = 'a+' if append else 'w'
    with open(output_path, mode, encoding='utf-8') as f:
        for line in data:
            json_record = json.dumps(line, ensure_ascii=False)
            f.write(json_record + '\n')
    print('Wrote {} records to {}'.format(len(data), output_path))

def load_jsonl(input_path) -> list:
    """
    Read list of objects from a JSON lines file.
    """
    data = []
    with open(input_path, 'r', encoding='utf-8') as f:
        for line in f: …
Run Code Online (Sandbox Code Playgroud)

python jsonlines

0
推荐指数
1
解决办法
1631
查看次数

标签 统计

jsonlines ×1

python ×1