| import json |
| import random |
| import os |
|
|
| |
| |
| INPUT_FILE_PATH = "/root/test/weitiao/LLaMA-Factory/data/instag2100_BCD_sharegpt.json" |
| |
| OUTPUT_FILE_PATH = "/root/test/weitiao/LLaMA-Factory/data/instag2100_BCD_sharegpt_shuffled.json" |
| |
|
|
| def shuffle_json_file_to_new_path(input_file_path, output_file_path): |
| """ |
| 读取 JSON 文件(假设内容是一个列表),打乱列表顺序,然后写入到新的文件路径。 |
| """ |
| print(f"开始处理输入文件: {input_file_path}") |
|
|
| |
| if not os.path.exists(input_file_path): |
| print(f"错误: 输入文件不存在! 路径: {input_file_path}") |
| return |
|
|
| try: |
| |
| with open(input_file_path, 'r', encoding='utf-8') as f: |
| |
| data = json.load(f) |
|
|
| print(f"成功读取 {len(data)} 条语料。") |
|
|
| |
| if isinstance(data, list): |
| |
| random.shuffle(data) |
| print("语料顺序已成功打乱。") |
| else: |
| print(f"警告: 文件内容不是一个列表 (list),而是 {type(data)}。无法进行打乱操作。") |
| return |
|
|
| |
| |
| with open(output_file_path, 'w', encoding='utf-8') as f: |
| |
| json.dump(data, f, ensure_ascii=False, indent=4) |
|
|
| print(f"处理完成。打乱后的语料已成功写入新文件: {output_file_path}") |
|
|
| except json.JSONDecodeError: |
| print("错误: JSON 文件格式不正确,无法解析。请检查文件内容。") |
| except Exception as e: |
| print(f"处理过程中发生未知错误: {e}") |
|
|
| |
| shuffle_json_file_to_new_path(INPUT_FILE_PATH, OUTPUT_FILE_PATH) |