#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import re def load_visual_design_data(): """加载视觉设计岗位简历数据""" with open('网页未导入数据/视觉设计产业/视觉设计岗位简历.json', 'r', encoding='utf-8') as f: return json.load(f) def extract_questions_from_content(content): """从面试题内容中提取问题和答案,保留原始格式""" questions = [] # 分割成三个部分:专业能力类、行为经历类、情景应变类 sections = re.split(r'#\s*[一二三]、\s*(专业能力类|行为经历类|情景应变类)', content) current_section = "专业能力类" for i in range(1, len(sections), 2): if i < len(sections): section_name = sections[i] section_content = sections[i+1] if i+1 < len(sections) else "" # 提取该部分的问题 pattern = r'(\d+)\.\s*问题[::]?\s*(.*?)(?:参考回答[::]?|回答[::]?)(.*?)(?=\d+\.\s*问题|$)' matches = re.findall(pattern, section_content, re.DOTALL) for match in matches: q_num = match[0] question_text = match[1].strip() answer_text = match[2].strip() # 保留答案中的换行和格式 questions.append({ "id": f"{section_name}_q{q_num}", "question": question_text, "answer": answer_text, "category": section_name }) return questions def update_mock_file(): """直接更新resumeInterviewMock.js文件""" # 加载视觉设计数据 visual_data = load_visual_design_data() # 读取mock文件 with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: mock_content = f.read() # 为每个岗位更新面试题 for item in visual_data: position_name = item.get('岗位名称', '') interview_content = item.get('面试题内容', '') if not position_name or not interview_content: continue # 提取面试题 questions = extract_questions_from_content(interview_content) if not questions: continue # 查找该岗位在mock文件中的位置 # 使用更精确的模式匹配 pattern = rf'(name:\s*["\']?{re.escape(position_name)}["\']?.*?questions:\s*\[)(.*?)(\])' def replace_questions(match): prefix = match.group(1) suffix = match.group(3) # 构建新的questions数组内容 questions_str = "" for i, q in enumerate(questions[:10]): # 限制最多10个问题 if i > 0: questions_str += ",\n " # 转义问题和答案中的特殊字符 question_escaped = q['question'].replace('"', '\\"').replace('\n', '\\n') answer_escaped = q['answer'].replace('"', '\\"').replace('\n', '\\n') questions_str += f'''{{ "id": "{q['id']}", "question": "{question_escaped}", "answer": "{answer_escaped}" }}''' return prefix + "\n " + questions_str + "\n " + suffix # 尝试替换 new_content = re.sub(pattern, replace_questions, mock_content, flags=re.DOTALL) if new_content != mock_content: mock_content = new_content print(f"已更新岗位:{position_name}") # 写回文件 with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(mock_content) print("更新完成!") if __name__ == '__main__': update_mock_file()