|
|
import json |
|
|
import os |
|
|
|
|
|
def split_data(input_file, output_dir, split_size=2500): |
|
|
""" |
|
|
将 JSON 文件中的数据划分为每个 split 包含指定数量的条目。 |
|
|
|
|
|
:param input_file: 输入的 JSON 文件路径 |
|
|
:param output_dir: 输出目录,用于保存分割后的文件 |
|
|
:param split_size: 每个 split 的大小,默认为 2500 |
|
|
""" |
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
|
|
|
|
|
with open(input_file, 'r', encoding='utf-8') as f: |
|
|
data = json.load(f) |
|
|
|
|
|
|
|
|
if not isinstance(data, list): |
|
|
raise ValueError("JSON 数据必须是一个列表") |
|
|
|
|
|
|
|
|
total_count = len(data) |
|
|
num_splits = total_count // split_size |
|
|
|
|
|
print(f"总数据量: {total_count}, 分割数量: {num_splits}") |
|
|
|
|
|
|
|
|
for i in range(num_splits): |
|
|
start_index = i * split_size |
|
|
end_index = (i + 1) * split_size |
|
|
if i == num_splits - 1: |
|
|
split_data = data[start_index:] |
|
|
else: |
|
|
split_data = data[start_index:end_index] |
|
|
|
|
|
|
|
|
output_file = os.path.join(output_dir, f"split_{i + 1}.json") |
|
|
|
|
|
|
|
|
with open(output_file, 'w', encoding='utf-8') as f: |
|
|
json.dump(split_data, f, ensure_ascii=False, indent=4) |
|
|
|
|
|
print(f"已保存 {output_file},包含 {len(split_data)} 条数据") |
|
|
|
|
|
|
|
|
input_file = "/share/project/sunshuang/deep_search/data_syn/data/mixed_data/mixed_data_all.json" |
|
|
output_dir = "/share/project/sunshuang/deep_search/data_syn/data/mixed_data/splits" |
|
|
split_data(input_file, output_dir) |