Files
ALL-teach_sys/frontend_化工/fix_json_syntax.py

169 lines
6.9 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import re
def fix_json_syntax_error():
"""修复JSON语法错误正确处理换行符"""
# 读取原文件
with open('src/pages/ProjectLibraryPage/index.jsx', 'r', encoding='utf-8') as f:
content = f.read()
# 读取学生完成的项目数据
with open('网页未导入数据/学生完成的项目.json', 'r', encoding='utf-8') as f:
data = json.load(f)
# 提取化工项目数据
chemical_projects = []
for item in data:
if item.get('所属就业管家') == '化工':
unit_name = item.get('单元名称查询', '')
ai_projects = item.get('AI项目名称', [])
if unit_name and ai_projects:
cleaned_projects = []
for project in ai_projects:
cleaned_project = re.sub(r'^\d+\.', '', project).strip()
cleaned_projects.append(cleaned_project)
chemical_projects.append({
'unitName': unit_name,
'projects': cleaned_projects
})
# 读取markdown文件内容
try:
with open('网页未导入数据/化工产业/学生完成的项目/化工储罐液位自动补水与搅拌联锁控制.md', 'r', encoding='utf-8') as f:
md_content = f.read()
# 解析markdown内容为sections
sections = []
lines = md_content.split('\n')
current_section = None
current_content = []
for line in lines:
if line.startswith('# '):
if current_section:
# 正确处理换行符和引号
section_content = '\n'.join(current_content).strip()
# 转义引号和换行符
section_content = section_content.replace('"', '\\"').replace('\n', '\\n')
sections.append({
'title': current_section,
'content': section_content
})
current_section = line[2:].strip()
current_content = []
elif current_section:
current_content.append(line)
# 添加最后一个section
if current_section:
section_content = '\n'.join(current_content).strip()
section_content = section_content.replace('"', '\\"').replace('\n', '\\n')
sections.append({
'title': current_section,
'content': section_content
})
# 创建可点击项目数据使用正确的JSON格式
clickable_project = {
"id": "clickable-1",
"name": "化工储罐液位自动补水与搅拌联锁控制项目",
"unitName": "化工PLC可编程逻辑控制系统",
"isClickable": True,
"content": {
"title": "化工储罐液位自动补水与搅拌联锁控制项目",
"description": "本项目基于PLC可编程逻辑控制器实现化工储罐液位的自动监控、补水控制以及搅拌器的联锁保护功能。",
"images": [],
"sections": sections
}
}
except FileNotFoundError:
clickable_project = {
"id": "clickable-1",
"name": "化工储罐液位检测与泵组启停PLC控制项目",
"unitName": "化工PLC电气控制",
"isClickable": True,
"content": {
"title": "化工储罐液位检测与泵组启停PLC控制项目",
"description": "基于PLC实现化工储罐液位自动检测与泵组联动控制确保生产过程安全稳定。",
"images": [],
"sections": [
{
"title": "项目概述",
"content": "本项目实现了化工储罐液位的自动检测与泵组启停的PLC控制系统通过液位传感器实时监测储罐状态自动控制进料泵和出料泵的运行确保化工生产过程的安全性和稳定性。"
}
]
}
}
# 生成myProjectsData JavaScript代码
my_projects_js = "const myProjectsData = [\n"
for i, project in enumerate(chemical_projects):
my_projects_js += " {\n"
my_projects_js += f' "unitName": "{project["unitName"]}",\n'
my_projects_js += ' "projects": [\n'
for j, proj_name in enumerate(project["projects"]):
# 转义引号
escaped_name = proj_name.replace('"', '\\"')
my_projects_js += f' "{escaped_name}"'
if j < len(project["projects"]) - 1:
my_projects_js += ","
my_projects_js += "\n"
my_projects_js += " ]\n"
my_projects_js += " }"
if i < len(chemical_projects) - 1:
my_projects_js += ","
my_projects_js += "\n"
my_projects_js += "];"
# 生成clickableProjects JavaScript代码
clickable_js = "const clickableProjects = [\n"
clickable_js += " {\n"
clickable_js += f' "id": "{clickable_project["id"]}",\n'
clickable_js += f' "name": "{clickable_project["name"]}",\n'
clickable_js += f' "unitName": "{clickable_project["unitName"]}",\n'
clickable_js += f' "isClickable": {str(clickable_project["isClickable"]).lower()},\n'
clickable_js += ' "content": {\n'
clickable_js += f' "title": "{clickable_project["content"]["title"]}",\n'
clickable_js += f' "description": "{clickable_project["content"]["description"]}",\n'
clickable_js += ' "images": [],\n'
clickable_js += ' "sections": [\n'
for i, section in enumerate(clickable_project["content"]["sections"]):
clickable_js += ' {\n'
clickable_js += f' "title": "{section["title"]}",\n'
clickable_js += f' "content": "{section["content"]}"\n'
clickable_js += ' }'
if i < len(clickable_project["content"]["sections"]) - 1:
clickable_js += ","
clickable_js += "\n"
clickable_js += ' ]\n'
clickable_js += ' }\n'
clickable_js += ' }\n'
clickable_js += "];"
# 替换内容
# 替换myProjectsData
pattern1 = r'const myProjectsData = \[[\s\S]*?\];'
content = re.sub(pattern1, my_projects_js, content)
# 替换clickableProjects
pattern2 = r'const clickableProjects = \[[\s\S]*?\];'
content = re.sub(pattern2, clickable_js, content)
# 写入文件
with open('src/pages/ProjectLibraryPage/index.jsx', 'w', encoding='utf-8') as f:
f.write(content)
print("✅ 已修复JSON语法错误")
print(f"✅ 更新了 {len(chemical_projects)} 个化工单元的数据")
print("✅ 修复了字符串转义问题")
if __name__ == "__main__":
fix_json_syntax_error()