上传文件
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
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"project2json/outputs/json/220kV变电站工程.json"
|
||||
|
||||
# 调用转换函数
|
||||
convert_json_to_readable(input_file)
|
||||
Reference in New Issue
Block a user