File size: 1,803 Bytes
4ac1fc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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 数据必须是一个列表")
    
    # 计算总数据量和需要的 split 数量
    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)