Python 题库数据处理:统计、查询与答案判断全攻略
```python import os import codecs # 读取题库文件的函数 def read_question_bank(file_path, encoding='gbk'): """ 读取指定路径的题库文件 参数: file_path (str): 题库文件的路径 encoding (str): 文件的编码方式 返回: data (list): 题库内容的列表 """ with codecs.open(file_path, 'r', encoding=encoding) as f: data = f.readlines() return data # 统计题目类型和数量的函数 def count_question_types(data): """ 统计题目类型和数量 参数: data (list): 题库内容的列表 返回: result (dict): 统计结果的字典 """ result = {} for line in data: # 假设题目格式为 "章名: 题型 题目内容" parts = line.split(': ') if len(parts) == 2: chapter, question_info = parts question_type = question_info.split(' ')[0] if chapter in result: if question_type in result[chapter]: result[chapter][question_type] += 1 else: result[chapter][question_type] = 1 else: result[chapter] = {question_type: 1} return result # 根据用户输入输出指定题目 def output_question(data, chapter, question_type, question_num): """ 根据用户输入输出指定题目 参数: data (list): 题库内容的列表 chapter (str): 章名 question_type (str): 题型 question_num (int): 题目序号 返回: question (str): 指定的题目 """ start_index = 0 for i, line in enumerate(data): parts = line.split(': ') if len(parts) == 2 and parts[0] == chapter + ':' and parts[1].split(' ')[0] == question_type: start_index = i break for i in range(start_index, len(data)): line_parts = data[i].split(': ') if len(line_parts) == 2 and line_parts[1].split(' ')[0] == question_type: question_num -= 1 if question_num == 0: return line_parts[1].strip() return "未找到指定题目" # 根据用户输入输出指定题目答案 def output_answer(data, chapter, question_type, question_num): """ 根据用户输入输出指定题目答案 参数: data (list): 题库内容的列表 chapter (str): 章名 question_type (str): 题型 question_num (int): 题目序号 返回: answer (str): 指定题目的答案 """ question = output_question(data, chapter, question_type, question_num) for line in data: if question in line: return line.split('答案:')[1].strip() return "未找到指定题目答案" # 根据用户输入判断答案是否正确 def judge_answer(data, chapter, question_type, question_num, user_answer): """ 根据用户输入判断答案是否正确 参数: data (list): 题库内容的列表 chapter (str): 章名 question_type (str): 题型 question_num (int): 题目序号 user_answer (str): 用户输入的答案 返回: result (str): 判断结果(正确或错误) """ correct_answer = output_answer(data, chapter, question_type, question_num) if user_answer == correct_answer: return "正确" else: return "错误" # 主函数 def main(): file_path = os.path.join('/data/share_files/experiment_file/e_16545/datafiles', 'python 题库(考试用)') data = read_question_bank(file_path) # 统计题目类型和数量 result = count_question_types(data) for chapter, types in result.items(): print(f"{chapter}:") for question_type, count in types.items(): print(f" {question_type} {count} 道") # 示例:输出指定题目、答案及判断 chapter = "第二章" question_type = "判断题" question_num = 6 print(f"题目:{output_question(data, chapter, question_type, question_num)}") print(f"答案:{output_answer(data, chapter, question_type, question_num)}") user_answer = "示例答案" print(f"判断结果:{judge_answer(data, chapter, question_type, question_num, user_answer)}") if __name__ == "__main__": main() ``` 1. 确保文件路径的正确性,避免因路径错误导致无法读取题库文件。 2. 处理用户输入时,要进行必要的输入验证,例如章名、题型和题目序号的格式和有效性。 3. 对于答案的判断,要考虑到答案的多样性和可能的模糊性,尽量准确判断用户答案的正确性。 ######[AI写代码神器 | 1210点数解答 | 2024-12-20 16:28:22]
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)458
- Java实现:C1科目一模拟考试系统,功能全面助力高效备考! (GPT | 555点数解答 | 2024-12-09 10:49:37)266
- Java 实现 C1 科目一模拟考试系统:集题库管理、多样练习与成绩统计于一体 (GPT | 36点数解答 | 2024-12-13 17:35:26)182
- Python实战:爬取西部证券股票数据并多方法分析(字节豆包 | 744点数解答 | 2024-11-11 20:45:55)139
- Python 实战:学生信息列表转字典、遍历、深拷贝及数据修改全流程(GPT | 422点数解答 | 2024-10-29 15:43:54)414
- Python实战:学生成绩列表转字典,深拷贝与数据修改操作全解析(字节豆包 | 254点数解答 | 2024-10-29 16:01:39)405
- 补全含错误代码:Vue结合Element UI组件实现查询、新增及表格展示功能 ( | 400点数解答 | 2023-12-19 15:20:50)432
- 单线程频繁查ClickHouse致CPU飙升?这些优化方案速看!(DeepSeek | 491点数解答 | 2025-02-13 17:49:54)228
- MySQL:巧用 NOT LIKE 查询字段不包含特定字符串及注意要点 (DeepSeek | 264点数解答 | 2025-02-13 18:25:01)179
- Python 实现超有趣大鱼吃小鱼游戏,从初始 1 级开启进化之旅!( | 767点数解答 | 2024-05-31 11:50:29)248
- Python 实现移位与仿射密码:精准加密解密及关键逻辑解析(字节豆包 | 798点数解答 | 2024-12-18 15:55:47)250
- Java调用Python接口中文乱码?设置UTF - 8编码一招解决!(讯飞星火 | 263点数解答 | 2024-06-06 17:07:59)389