File size: 583 Bytes
8ba9801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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")