xasoss / replace_null.py
Jack Lee
jsonl中的null替换成“”
8ba9801
Raw
History Blame Contribute Delete
583 Bytes
import json, glob
from pathlib import Path
# 当前目录及子目录
files = list(Path(".").rglob("*.jsonl")) + list(Path(".").rglob("*.csv"))
# 转成字符串路径
files = [str(f) for f in files]
for file in files:
with open(file, "r", encoding="utf-8") as f:
data = [json.loads(line) for line in f]
# 替换 null
for d in data:
for k in d:
if d[k] is None:
d[k] = ""
# 写回
with open(file, "w", encoding="utf-8") as f:
for line in data:
f.write(json.dumps(line, ensure_ascii=False) + "\n")