43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
# -*- encoding:utf-8 -*-
|
|
|
|
'''
|
|
@Author : dingjiawen
|
|
@Date : 2024/03/20 15:30
|
|
@Usage :
|
|
@Desc :将yaml文件转为json的
|
|
'''
|
|
|
|
import yaml
|
|
import json
|
|
import os
|
|
|
|
raw_path = "./raw"
|
|
output_path = "./output"
|
|
for root, dirs, files in os.walk(raw_path):
|
|
index = 4
|
|
for file in files:
|
|
file_name = os.path.join(raw_path, file)
|
|
# 读取YAML文件
|
|
with open(file_name, 'r', encoding='utf-8') as yaml_file:
|
|
yaml_data = yaml.safe_load(yaml_file)
|
|
# yaml_data = {}
|
|
|
|
# 将YAML数据转换为JSON字符串
|
|
# json_data = json.dumps(yaml_data, indent=4)
|
|
for each_data in yaml_data:
|
|
each_data['id'] = index
|
|
each_data['catelog'] = file.split('-')[0]
|
|
each_data['logo'] = each_data['img']
|
|
each_data['desc'] = each_data['description']
|
|
each_data['sort'] = 1
|
|
each_data['hide'] = False
|
|
each_data.pop('img')
|
|
each_data.pop('description')
|
|
index += 1
|
|
# 打印JSON数据
|
|
print(yaml_data)
|
|
# 如果你想将JSON数据保存到文件
|
|
file_name = os.path.join(output_path, file.split('.')[0] + '.json')
|
|
with open(file_name, 'w', encoding='utf-8') as json_file:
|
|
json.dump(yaml_data, json_file, ensure_ascii=False)
|