#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 清洁版本:完整更新所有岗位群的面试题 从智能制造岗位简历.json中提取完整数据 """ import json import re from datetime import datetime print("=== 完整更新所有岗位群面试题(清洁版) ===\n") # 1. 读取源数据 print("1. 读取智能制造岗位简历.json...") with open('网页未导入数据/智能制造产业/智能制造岗位简历.json', 'r', encoding='utf-8') as f: source_data = json.load(f) # 2. 解析所有面试题(只保留每个岗位群的第一个) print("2. 解析面试题数据...") interview_questions_map = {} processed_groups = set() for item in source_data: job_group = item.get('简历岗位群', '') interview_content = item.get('面试题内容', '') if not job_group or not interview_content or job_group in processed_groups: continue processed_groups.add(job_group) # 收集所有问题 all_questions = [] lines = interview_content.split('\n') i = 0 while i < len(lines): line = lines[i].strip() # 匹配问题编号 question_match = re.match(r'^(\d+)[\.、]\s*(.+)', line) if question_match: question_text = question_match.group(2).strip() # 查找答案 answer = "" i += 1 while i < len(lines): next_line = lines[i].strip() if re.match(r'^\d+[\.、]\s*', next_line) or next_line.startswith('#'): i -= 1 break if next_line and (next_line.startswith('答案') or next_line.startswith('示例答案')): answer = re.sub(r'^(答案|示例答案)[::]?\s*', '', next_line).strip() if not answer: i += 1 if i < len(lines): answer = lines[i].strip() break elif next_line and not answer: answer = next_line break i += 1 if not answer: answer = "请根据实际情况回答" all_questions.append({ 'question': question_text, 'answer': answer }) i += 1 if all_questions: interview_questions_map[job_group] = all_questions print(f" - {job_group}: {len(all_questions)} 道题") print(f"\n共解析 {len(interview_questions_map)} 个岗位群") # 3. 读取当前mock文件 print("\n3. 读取当前mock文件...") with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: mock_content = f.read() # 4. 备份 backup_time = datetime.now().strftime('%Y%m%d_%H%M%S') backup_file = f'src/mocks/resumeInterviewMock.js.backup_clean_{backup_time}' print(f"4. 创建备份: {backup_file}") with open(backup_file, 'w', encoding='utf-8') as f: f.write(mock_content) # 5. 处理每个岗位群 print("\n5. 开始更新各岗位群...") successful_updates = [] failed_updates = [] # 处理顺序(按在文件中出现的顺序) job_order = [ "3D打印工艺", "CNC加工工艺", "PLC", "钣金工程", "产品测试", "产品设计", "电气设计", "非标自动化设计", "工业机器人", "焊接工艺", "机加工工艺", "机器视觉", "模具工程", "塑性成形工艺", "特种加工工艺", "自动化控制" ] for job_group in job_order: if job_group not in interview_questions_map: print(f" ⚠️ {job_group} 没有面试题数据") continue questions = interview_questions_map[job_group] print(f"\n处理 {job_group} ({len(questions)} 道题)...") # 构建questions数组 sub_questions_list = [] for idx, q in enumerate(questions, 1): question_text = q['question'].replace('\\', '\\\\').replace('"', '\\"') answer_text = q['answer'].replace('\\', '\\\\').replace('"', '\\"').replace('\n', ' ') sub_questions_list.append(f'''{{ "id": "q1_{idx}", "question": "{question_text}", "answer": "{answer_text}" }}''') new_questions_str = f'''[ {{ "id": "group_q1", "question": "# {job_group}岗位面试题", "subQuestions": [ {','.join(sub_questions_list)} ] }} ]''' # 查找并替换 # 查找该岗位群的起始位置 pattern = f'"name": "{job_group}"' start_pos = mock_content.find(pattern) if start_pos == -1: print(f" ❌ 未找到岗位群: {job_group}") failed_updates.append(job_group) continue # 找到后面的questions字段 questions_pos = mock_content.find('"questions":', start_pos) # 检查是否在合理范围内(避免找到其他岗位群的questions) next_name_pos = mock_content.find('"name":', start_pos + len(pattern)) if next_name_pos != -1 and questions_pos > next_name_pos: print(f" ❌ {job_group} 的questions字段超出范围") failed_updates.append(job_group) continue if questions_pos == -1 or questions_pos - start_pos > 5000: print(f" ❌ 未找到 {job_group} 的questions字段") failed_updates.append(job_group) continue # 找到questions数组的起始[ array_start = mock_content.find('[', questions_pos) if array_start == -1: print(f" ❌ 未找到 {job_group} 的数组起始") failed_updates.append(job_group) continue # 找到对应的结束] bracket_count = 1 pos = array_start + 1 array_end = -1 while pos < len(mock_content) and bracket_count > 0: if mock_content[pos] == '[': bracket_count += 1 elif mock_content[pos] == ']': bracket_count -= 1 if bracket_count == 0: array_end = pos break pos += 1 if array_end == -1: print(f" ❌ 未找到 {job_group} 的数组结束") failed_updates.append(job_group) continue # 执行替换 before = mock_content[:array_start] after = mock_content[array_end + 1:] mock_content = before + new_questions_str + after print(f" ✅ 成功更新 {job_group}: {len(questions)} 道题") successful_updates.append((job_group, len(questions))) # 6. 保存文件 print(f"\n6. 保存更新后的文件...") with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(mock_content) # 7. 验证语法 print("\n7. 验证语法...") import subprocess result = subprocess.run(['node', '-c', 'src/mocks/resumeInterviewMock.js'], capture_output=True, text=True) if result.returncode == 0: print("✅ 语法检查通过!") print(f"\n=== 更新完成 ===") print(f"成功: {len(successful_updates)} 个岗位群") print(f"失败: {len(failed_updates)} 个岗位群") if successful_updates: print("\n✅ 成功更新的岗位群:") for group, count in successful_updates: print(f" - {group}: {count} 道题") if failed_updates: print("\n❌ 更新失败的岗位群:") for group in failed_updates: print(f" - {group}") # 验证最终结果 print("\n=== 验证最终结果 ===") with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: final_content = f.read() for group, expected_count in successful_updates: # 查找该岗位群 group_pos = final_content.find(f'"name": "{group}"') if group_pos != -1: # 向后查找最近的questions questions_pos = final_content.find('"questions":', group_pos) if questions_pos != -1: # 统计该区域内的题目数量 search_area = final_content[questions_pos:questions_pos + 20000] # 找到下一个name或文件结束 next_name = search_area.find('"name":') if next_name != -1: search_area = search_area[:next_name] actual_count = len(re.findall(r'"id": "q1_\d+"', search_area)) status = "✅" if actual_count == expected_count else f"⚠️" print(f" {status} {group}: {actual_count}/{expected_count} 道题") else: print("❌ 语法错误:") print(result.stderr) print("\n正在恢复备份...") with open(backup_file, 'r', encoding='utf-8') as f: mock_content = f.read() with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(mock_content) print("已恢复到备份版本")