#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 直接更新所有岗位群的面试题 使用更精确的文本处理方法 """ 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 = {} 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 interview_questions_map: continue # 跳过已处理的岗位群 # 收集所有问题 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_direct_{backup_time}' print(f"4. 创建备份: {backup_file}") with open(backup_file, 'w', encoding='utf-8') as f: f.write(mock_content) # 5. 逐个岗位群更新 print("\n5. 开始更新各岗位群...") updated_count = 0 for job_group, questions in interview_questions_map.items(): print(f"\n处理 {job_group}...") # 构建新的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}" }}''') # 构建完整的questions结构 new_questions_str = f'''[ {{ "id": "group_q1", "question": "# {job_group}岗位面试题", "subQuestions": [ {','.join(sub_questions_list)} ] }} ]''' # 使用更精确的查找和替换 # 先找到岗位群的位置 job_group_pattern = f'"name": "{job_group}"' job_group_pos = mock_content.find(job_group_pattern) if job_group_pos == -1: print(f" ⚠️ 未找到 {job_group}") continue # 从该位置开始查找 questions 字段 search_start = job_group_pos questions_start = mock_content.find('"questions":', search_start) if questions_start == -1 or questions_start - job_group_pos > 5000: # 限制搜索范围 print(f" ⚠️ 未找到 {job_group} 的questions字段") continue # 找到 questions 数组的开始 [ array_start = mock_content.find('[', questions_start) if array_start == -1: print(f" ⚠️ 未找到 {job_group} 的questions数组开始") 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} 的questions数组结束") continue # 替换questions数组 before = mock_content[:array_start] after = mock_content[array_end + 1:] mock_content = before + new_questions_str + after print(f" ✅ {job_group} 已更新为 {len(questions)} 道题") updated_count += 1 # 6. 保存文件 print(f"\n6. 保存更新后的文件...") print(f" 共更新 {updated_count} 个岗位群") 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"成功更新 {updated_count}/{len(interview_questions_map)} 个岗位群") # 统计最终结果 print("\n=== 最终统计 ===") with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: final_content = f.read() for group in interview_questions_map.keys(): # 查找该岗位群的questions 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 and questions_pos - group_pos < 5000: # 统计该区域的题目数量 search_end = questions_pos + 10000 # 限制搜索范围 section = final_content[questions_pos:min(search_end, len(final_content))] count = len(re.findall(r'"id": "q1_\d+"', section)) expected = len(interview_questions_map[group]) status = "✅" if count == expected else f"⚠️ ({count}/{expected})" print(f" {status} {group}: {count} 道题") else: print(f" ❌ {group}: 未找到questions") else: print(f" ❌ {group}: 未找到岗位群") 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("已恢复到备份版本")