61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
"""
|
|||
|
|
清理混合的数据结构,移除所有嵌套的subQuestions结构
|
|||
|
|
只保留扁平的questions结构
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import re
|
|||
|
|
|
|||
|
|
def clean_mixed_structures():
|
|||
|
|
"""清理混合结构"""
|
|||
|
|
try:
|
|||
|
|
# 读取文件
|
|||
|
|
with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f:
|
|||
|
|
content = f.read()
|
|||
|
|
|
|||
|
|
print("开始清理混合结构...")
|
|||
|
|
|
|||
|
|
# 1. 移除所有包含subQuestions的问题组
|
|||
|
|
# 匹配从 { "id": "group_q..." 开始到对应的 } 结束的完整问题组
|
|||
|
|
content = re.sub(
|
|||
|
|
r',?\s*\{\s*"id":\s*"group_q\d+"[\s\S]*?"subQuestions":\s*\[[\s\S]*?\]\s*\}',
|
|||
|
|
'',
|
|||
|
|
content,
|
|||
|
|
flags=re.DOTALL
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 2. 清理残留的多余逗号和空白
|
|||
|
|
content = re.sub(r',\s*,', ',', content)
|
|||
|
|
content = re.sub(r'\[\s*,', '[', content)
|
|||
|
|
content = re.sub(r',\s*\]', ']', content)
|
|||
|
|
|
|||
|
|
# 3. 修复空的questions数组
|
|||
|
|
content = re.sub(r'"questions":\s*\[\s*\]', '"questions": []', content)
|
|||
|
|
|
|||
|
|
# 4. 清理孤立的逗号和括号
|
|||
|
|
content = re.sub(r'\s*\]\s*\}\s*\]\s*\},', '\n }', content)
|
|||
|
|
|
|||
|
|
# 5. 修复岗位间的连接
|
|||
|
|
content = re.sub(r'(\s+\}\s*)\n\s+\{\s*("id":\s*"health_)', r'\1,\n {\n \2', content)
|
|||
|
|
|
|||
|
|
print("清理完成,写入文件...")
|
|||
|
|
|
|||
|
|
# 写回文件
|
|||
|
|
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(content)
|
|||
|
|
|
|||
|
|
print("混合结构清理完成!")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"清理失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
clean_mixed_structures()
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|