最简单做网站,杭州小程序推广,哈尔滨专业网站建设公司,服装订单接单网站文章目录 读取长篇幅的 jsonl 文件时#xff0c;我们难以了解 json 的格式#xff0c;复制出来贴到 sojson 之类的网站#xff0c;当数据量大的时候感觉麻烦。 不如自己写个 json 格式美化#xff0c;然后保存到文件。
text open(file_path).readline() # 读取 jsonl 文… 文章目录 读取长篇幅的 jsonl 文件时我们难以了解 json 的格式复制出来贴到 sojson 之类的网站当数据量大的时候感觉麻烦。 不如自己写个 json 格式美化然后保存到文件。
text open(file_path).readline() # 读取 jsonl 文件的一行及一个json 字符串
d json.loads(text.strip()) # 加载为字典
json_str json.dumps(d, indent4, ensure_asciiFalse) # 打印缩进为 4个字符可现实中文读取2级文件夹中对应的 jsonl 文件
#!/usr/bin/env python
# -*- encoding: utf-8 -*-import os
import sys
import json root_dir /home/xxx/dir1
save_dir os.path.join(root_dir, short)
if not os.path.isdir(save_dir):os.makedirs(save_dir) for ym in os.listdir(root_dir): ym_dir os.path.join(root_dir, ym)if not os.path.isdir(ym_dir):continue for dir_name in os.listdir(ym_dir):dir os.path.join(ym_dir, dir_name) if not os.path.isdir(dir):continue for file_name in os.listdir(dir): file_path os.path.join(dir, file_name) text open(file_path).readline() d json.loads(text.strip()) json_str json.dumps(d, indent4, ensure_asciiFalse)print(file_name)save_path os.path.join(save_dir, f{ym}_{dir_name}_{file_name})print(save_path)with open(save_path, w) as f:f.write(json_str.strip()) 2024-06-05三