merge_json.py
1 import json 2 import os 3 4 data_dir = r'E:\Project\Python\MemoTrace\data\聊天记录' 5 6 dev_res = [] 7 train_res = [] 8 9 for filepath, dirnames, filenames in os.walk(data_dir): 10 for filename in filenames: 11 if filename.endswith('.json'): 12 print(filename, filepath) 13 filepath_ = os.path.join(filepath, filename) 14 with open(filepath_, 'r', encoding='utf-8') as f: 15 data = json.load(f) 16 if data: 17 if filename.endswith('train.json'): 18 train_res += data 19 else: 20 dev_res += data 21 22 with open('train.json', 'w', encoding='utf-8') as f: 23 json.dump(train_res, f, ensure_ascii=False, indent=4) 24 25 with open('dev.json', 'w', encoding='utf-8') as f: 26 json.dump(dev_res, f, ensure_ascii=False, indent=4)