#!/usr/bin/env python3 import json import os from datetime import datetime # 读取视觉设计岗位简历.json文件获取面试题 json_file = '网页未导入数据/视觉设计产业/视觉设计岗位简历.json' mock_file = 'src/mocks/resumeInterviewMock.js' print("正在读取面试题数据...") with open(json_file, 'r', encoding='utf-8') as f: positions_data = json.load(f) # 提取包装设计师的面试题 package_questions = [] for position in positions_data: if position.get("岗位名称") == "包装设计师": interview_content = position.get("面试题内容", "") lines = interview_content.split('\n') current_question = None current_answer = '' collecting_answer = False for line in lines: line_stripped = line.strip() # 检测问题行 if line_stripped and line_stripped[0].isdigit() and ('问题' in line_stripped or ':' in line_stripped): # 保存上一个问题 if current_question: answer = current_answer.strip() if len(answer) > 400: cut_point = 400 for sep in ['。', ';', '. ']: pos = answer[:400].rfind(sep) if pos > 200: cut_point = pos + 1 break answer = answer[:cut_point] package_questions.append({ 'question': current_question, 'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。" }) # 提取新问题 if '问题:' in line_stripped: parts = line_stripped.split('问题:', 1) current_question = parts[1].strip() elif '问题:' in line_stripped: parts = line_stripped.split('问题:', 1) current_question = parts[1].strip() elif '. ' in line_stripped: parts = line_stripped.split('. ', 1) current_question = parts[1].strip() if len(parts) > 1 else "" current_answer = '' collecting_answer = False # 检测答案开始 elif any(marker in line for marker in ['参考回答:', '参考答案:', '答案:']): collecting_answer = True for marker in ['参考回答:', '参考答案:', '答案:']: if marker in line: answer_part = line.split(marker, 1)[1].strip() if answer_part: current_answer = answer_part break # 收集答案内容 elif collecting_answer and line.strip(): if (line_stripped and line_stripped[0].isdigit() and '问题' in line_stripped): collecting_answer = False elif line_stripped.startswith('#'): collecting_answer = False else: if current_answer: current_answer += ' ' current_answer += line.strip() # 保存最后一个问题 if current_question: answer = current_answer.strip() if len(answer) > 400: cut_point = 400 for sep in ['。', ';', '. ']: pos = answer[:400].rfind(sep) if pos > 200: cut_point = pos + 1 break answer = answer[:cut_point] package_questions.append({ 'question': current_question, 'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。" }) break # 限制为16个问题 if len(package_questions) > 16: package_questions = package_questions[:16] print(f"提取了包装设计的 {len(package_questions)} 个问题") # 创建备份 backup_file = f"{mock_file}.backup_manual_{datetime.now().strftime('%Y%m%d_%H%M%S')}" with open(mock_file, 'r', encoding='utf-8') as f: content = f.read() with open(backup_file, 'w', encoding='utf-8') as f: f.write(content) print(f"已创建备份:{backup_file}") # 构建新的questions数组 sub_questions = [] for i, qa in enumerate(package_questions, 1): sub_questions.append({ "id": f"q2_{i}", "question": qa['question'], "answer": qa['answer'] }) new_questions = [{ "id": "group_q2", "question": "# 包装设计面试题", "subQuestions": sub_questions }] # 查找包装设计的questions位置并替换 # 从 "name": "包装设计" 找到对应的 questions 字段 import re # 找到包装设计块的开始 package_start = content.find('"name": "包装设计"') if package_start > 0: # 找到这个块的questions字段 questions_start = content.find('"questions":', package_start) if questions_start > 0: # 找到questions数组的开始和结束 array_start = content.find('[', questions_start) # 找到对应的结束括号 bracket_count = 1 pos = array_start + 1 while bracket_count > 0 and pos < len(content): if content[pos] == '[': bracket_count += 1 elif content[pos] == ']': bracket_count -= 1 pos += 1 if bracket_count == 0: # 生成新的JSON内容 questions_json = json.dumps(new_questions, ensure_ascii=False, indent=2) # 调整缩进 lines = questions_json.split('\n') formatted_lines = [] for line in lines: if line.strip(): formatted_lines.append(' ' + line) else: formatted_lines.append('') questions_json = '\n'.join(formatted_lines).strip() # 替换内容 new_content = content[:array_start] + questions_json + content[pos-1:] # 写入文件 with open(mock_file, 'w', encoding='utf-8') as f: f.write(new_content) # 验证语法 result = os.popen(f'node -c {mock_file} 2>&1').read() if result: print(f"❌ 语法错误:{result}") # 恢复备份 with open(mock_file, 'w', encoding='utf-8') as f: f.write(content) print("已恢复备份") else: print("✓ 语法验证通过") print(f"成功更新包装设计的 {len(sub_questions)} 个面试题!") else: print("未能正确找到questions数组的结束位置") else: print("未找到questions字段") else: print("未找到包装设计产业")