Python 题库数据处理:统计、查询、判题全实现
以下是一个可能的 Python 代码实现示例,用于解决您描述的问题: ```python import os import codecs def read_and_process_data(): folder_path = '/data/share_files/experiment_file/e_16545/datafiles' file_name = 'python题库(考试用)' file_path = os.path.join(folder_path, file_name) with codecs.open(file_path, 'r', 'gbk') as file: data = file.readlines() chapter_dict = {} for line in data: # 假设题目格式为"章名 题型 题目内容" parts = line.split() if len(parts) >= 2: chapter = parts[0] question_type = parts[1] if chapter not in chapter_dict: chapter_dict[chapter] = {} if question_type not in chapter_dict[chapter]: chapter_dict[chapter][question_type] = 0 chapter_dict[chapter][question_type] += 1 for chapter, types_dict in chapter_dict.items(): print(f"{chapter}:") for question_type, count in types_dict.items(): print(f"{question_type} {count} 道") def output_specific_question(chapter, question_type, question_number): folder_path = '/data/share_files/experiment_file/e_16545/datafiles' file_name = 'python题库(考试用)' file_path = os.path.join(folder_path, file_name) with codecs.open(file_path, 'r', 'gbk') as file: data = file.readlines() target_question = None for line in data: # 假设题目格式为"章名 题型 题目序号 题目内容" parts = line.split() if len(parts) >= 4 and parts[0] == chapter and parts[1] == question_type and int(parts[2]) == question_number: target_question = parts[3:] break if target_question: print(' '.join(target_question)) else: print("未找到指定题目") def output_specific_answer(chapter, question_type, question_number): folder_path = '/data/share_files/experiment_file/e_16545/datafiles' file_name = 'python题库(考试用)' file_path = os.path.join(folder_path, file_name) with codecs.open(file_path, 'r', 'gbk') as file: data = file.readlines() target_answer = None for line in data: # 假设答案格式为"章名 题型 题目序号 答案" parts = line.split() if len(parts) >= 4 and parts[0] == chapter and parts[1] == question_type and int(parts[2]) == question_number: target_answer = parts[3] break if target_answer: print(target_answer) else: print("未找到指定题目的答案") def judge_answer(chapter, question_type, question_number, user_answer): folder_path = '/data/share_files/experiment_file/e_16545/datafiles' file_name = 'python题库(考试用)' file_path = os.path.join(folder_path, file_name) with codecs.open(file_path, 'r', 'gbk') as file: data = file.readlines() correct_answer = None for line in data: # 假设答案格式为"章名 题型 题目序号 答案" parts = line.split() if len(parts) >= 4 and parts[0] == chapter and parts[1] == question_type and int(parts[2]) == question_number: correct_answer = parts[3] break if correct_answer == user_answer: print("正确") else: print("错误") # 1. 可以添加异常处理,以处理文件读取失败等异常情况。 # 2. 代码中对题目和答案格式的假设较为简单,可以考虑更灵活和健壮的格式处理方式。 # 3. 对于数据的存储和处理,可以考虑使用数据库来提高效率和管理的便利性。 ``` ######[AI写代码神器 | 988点数解答 | 2024-12-25 10:38:25]
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)478
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)362
- Python实战:爬取西部证券股票数据并多方法分析(字节豆包 | 744点数解答 | 2024-11-11 20:45:55)146
- Python 实战:学生信息列表转字典、遍历、深拷贝及数据修改全流程(GPT | 422点数解答 | 2024-10-29 15:43:54)432
- Python实战:学生成绩列表转字典,深拷贝与数据修改操作全解析(字节豆包 | 254点数解答 | 2024-10-29 16:01:39)426
- 补全含错误代码:Vue结合Element UI组件实现查询、新增及表格展示功能 ( | 400点数解答 | 2023-12-19 15:20:50)444
- 单线程频繁查ClickHouse致CPU飙升?这些优化方案速看!(DeepSeek | 491点数解答 | 2025-02-13 17:49:54)240
- MySQL:巧用 NOT LIKE 查询字段不包含特定字符串及注意要点 (DeepSeek | 264点数解答 | 2025-02-13 18:25:01)188
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)333
- Java调用Python接口中文乱码?设置UTF - 8编码一招解决!(讯飞星火 | 263点数解答 | 2024-06-06 17:07:59)406
- 解决Java调用Python接口中文乱码问题:设置UTF - 8编码全攻略(讯飞星火 | 160点数解答 | 2024-06-06 17:18:39)429
- Java调用Python接口中文乱码问题:字符编码统一解决方案(讯飞星火 | 344点数解答 | 2024-06-06 17:19:55)542