40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
import json
|
|
|
|
|
|
# 读取 JSON 文件
|
|
def load_json(file_path):
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
|
|
|
|
# 按7:3比例将一个JSON文件分成两个
|
|
def split_json(input_file, output_file1, output_file2):
|
|
# 读取数据
|
|
data = load_json(input_file)
|
|
|
|
# 计算数据的分割点
|
|
split_point = int(len(data) * 0.7)
|
|
|
|
# 按比例分割数据
|
|
data_part1 = data[:split_point] # 前70%数据
|
|
data_part2 = data[split_point:] # 后30%数据
|
|
|
|
# 保存数据到两个文件
|
|
with open(output_file1, 'w', encoding='utf-8') as f1:
|
|
json.dump(data_part1, f1, ensure_ascii=False, indent=4)
|
|
|
|
with open(output_file2, 'w', encoding='utf-8') as f2:
|
|
json.dump(data_part2, f2, ensure_ascii=False, indent=4)
|
|
|
|
print(f"数据已按 7:3 比例分割并保存到 {output_file1} 和 {output_file2}")
|
|
|
|
|
|
# 输入的 JSON 文件路径
|
|
input_file = 'merged_data.json'
|
|
# 输出的两个文件路径
|
|
output_file1 = 'data_part1.json'
|
|
output_file2 = 'data_part2.json'
|
|
|
|
# 按 7:3 比例分割并保存
|
|
split_json(input_file, output_file1, output_file2)
|