import json from tqdm import tqdm import random import os import json import os import random from tqdm import tqdm # 设置随机种子以保证结果可复现 random.seed(0) def save_to_json(data, filename): """保存数据到 JSON 文件""" with open(filename, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=4) print(f"Saved to {filename}, data length: {len(data)}") def load_json(file_path): """从 JSON 文件加载数据""" with open(file_path, "r", encoding="utf-8") as f: data = json.load(f) print(f"Loaded from {file_path}, data length: {len(data)}") return data def save_questions_to_txt(data, output_txt_file): """将数据中的 Question 字段保存到 TXT 文件""" with open(output_txt_file, 'w', encoding='utf-8') as f: for item in data: question = item.get("Question", "") # 获取 Question 字段,默认为空字符串 if question: # 如果 Question 存在,则写入文件 f.write(f"idx: {item['idx']}\n") f.write(f"question: {item['Question']}\n\n") # 每个问题占一行 print(f"Questions saved to {output_txt_file}") # 输入和输出文件路径 input_file = "/opt/aps/workdir/sunshuang/deep_search/math_data/math_qwq_4524.json" # 替换为你的输入文件路径 # 从文件中随机筛选871条数据 data = load_json(input_file) selected_data = random.sample(data, 871) save_to_json(selected_data, "/opt/aps/workdir/sunshuang/deep_search/math_data/math_qwq_4524_selected_871.json")