| import json | |
| # 文件路径 | |
| file_path = 'transformed_datat.json' | |
| # 读取JSON数据 | |
| with open(file_path, 'r', encoding='utf-8') as file: | |
| data = json.load(file) | |
| # 按 label 分类数据 | |
| safe_data = [item for item in data if item['label'] == 'safe'] | |
| unsafe_data = [item for item in data if item['label'] == 'unsafe'] | |
| # 保存到不同的文件 | |
| with open('safe_data.json', 'w') as safe_file: | |
| json.dump(safe_data, safe_file, indent=4) | |
| with open('unsafe_data.json', 'w') as unsafe_file: | |
| json.dump(unsafe_data, unsafe_file, indent=4) | |
| print("Data has been saved to 'safe_data.json' and 'unsafe_data.json'") |