import json import os import yaml def generate_combinations(data, output_prefix): keys = list(data.keys()) values = [data[k] for k in keys] for v in zip(*values): combination = dict(zip(keys, v)) output_filename = "_".join([f"{k}_{v}" for k, v in combination.items()]) + ".json" output_path = os.path.join(output_prefix, output_filename) with open(output_path, "w") as f: json.dump(combination, f, indent=2)
def read_json_file(file_path):
with open(file_path, "r") as f:
return json.load(f)
if __name__ == "__main__":
# 读取 YAML 文件
with open("data.yml") as f:
data = yaml.safe_load(f)
# 生成所有组合的 JSON 文件
generate_combinations(data, "output/")
# 读取 JSON 文件示例
example_file_path = "output/name_Alice_age_20_city_Beijing.json"
example_data = read_json_file(example_file_path)
print(example_data)