import json import os def convert_json_to_readable(input_file, output_file=None): """ 将JSON文件转换为可读格式并保存 参数: input_file: 输入JSON文件路径 output_file: 输出文件路径,如果为None则自动生成 """ try: # 读取JSON文件 with open(input_file, "r", encoding="utf-8") as f: data = json.load(f) # 如果未指定输出文件,则自动生成 if output_file is None: base_name = os.path.splitext(input_file)[0] output_file = f"{base_name}_readable.json" # 以美化格式写入新文件 with open(output_file, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=4) print(f"转换成功!可读格式的文件已保存为: {output_file}") return output_file except Exception as e: print(f"转换过程中出现错误: {str(e)}") return None if __name__ == "__main__": # 指定输入文件路径 input_file = ( r"E:\文件\LLM_model\RAG\code\Engineering_data_KG-1\equipment_calculation\new\实际工程\主网预算线路.json" ) # 调用转换函数 convert_json_to_readable(input_file)