dashboard/backend/fix_json_format.py

81 lines
2.3 KiB
Python
Raw Permalink Normal View History

2025-06-09 14:59:40 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
修复JSON文件格式问题
将Python的None值替换为JSON标准的null值
使用方法:
python fix_json_format.py
"""
import json
import sys
import os
from pathlib import Path
def fix_json_file():
"""修复JSON文件中的None值问题"""
# 源文件和目标文件路径
source_file = Path(__file__).parent.parent / "src" / "assets" / "工程研究中心.json"
if not source_file.exists():
print(f"❌ 错误:找不到文件 {source_file}")
return False
try:
print(f"📖 正在读取文件: {source_file}")
# 读取文件内容
with open(source_file, 'r', encoding='utf-8') as f:
content = f.read()
print(f"✅ 文件读取成功,大小: {len(content)} 字符")
# 替换None为null
print("🔄 正在修复None值...")
fixed_content = content.replace(': None,', ': null,')
fixed_content = fixed_content.replace(': None}', ': null}')
fixed_content = fixed_content.replace(': None]', ': null]')
# 统计替换数量
none_count = content.count(': None,') + content.count(': None}') + content.count(': None]')
print(f"🔧 找到并修复了 {none_count} 个None值")
# 验证JSON格式
print("📝 正在验证JSON格式...")
try:
json.loads(fixed_content)
print("✅ JSON格式验证通过")
except json.JSONDecodeError as e:
print(f"❌ JSON格式仍有问题: {e}")
return False
# 保存修复后的文件
print("💾 正在保存修复后的文件...")
with open(source_file, 'w', encoding='utf-8') as f:
f.write(fixed_content)
print("🎉 文件修复完成!")
return True
except Exception as e:
print(f"❌ 修复失败: {str(e)}")
return False
def main():
"""主函数"""
print("🚀 开始修复JSON文件格式...")
print("=" * 50)
success = fix_json_file()
print("=" * 50)
if success:
print("🎉 修复成功!现在可以运行导入脚本了。")
else:
print("💥 修复失败!")
input("\n按回车键退出...")
if __name__ == "__main__":
main()