Python 题库数据处理:统计、查询与答案判断全攻略
以下是一个可能的 Python 代码示例,用于实现您描述的功能: ```python import os import codecs # 读取题库文件并进行统计 def read_and_statistic_data(): file_path = os.path.join('/data/share_files/experiment_file/e_16545/datafiles', 'python 题库(考试用)') with codecs.open(file_path, 'r', 'gbk') as file: content = file.read() chapters = content.split('\n\n') # 假设每章之间以两个换行符分隔 statistics = {} for chapter in chapters: chapter_lines = chapter.split('\n') chapter_name = chapter_lines[0].split(':')[0] # 提取章名 if chapter_name not in statistics: statistics[chapter_name] = {} for line in chapter_lines[1:]: if line: # 排除空行 question_type = line.split(' ')[0] # 假设题型在每行开头 if question_type not in statistics[chapter_name]: statistics[chapter_name][question_type] = 0 statistics[chapter_name][question_type] += 1 return statistics # 根据用户输入输出指定题目 def output_specific_question(chapter_name, question_type, question_number): file_path = os.path.join('/data/share_files/experiment_file/e_16545/datafiles', 'python 题库(考试用)') with codecs.open(file_path, 'r', 'gbk') as file: content = file.read() chapters = content.split('\n\n') for chapter in chapters: chapter_lines = chapter.split('\n') current_chapter_name = chapter_lines[0].split(':')[0] if current_chapter_name == chapter_name: start_index = 1 for line in chapter_lines[1:]: if line: current_question_type = line.split(' ')[0] if current_question_type == question_type: if start_index == question_number: return line start_index += 1 # 根据用户输入输出指定题目答案 def output_specific_answer(chapter_name, question_type, question_number): file_path = os.path.join('/data/share_files/experiment_file/e_16545/datafiles', 'python 题库(考试用)') with codecs.open(file_path, 'r', 'gbk') as file: content = file.read() chapters = content.split('\n\n') for chapter in chapters: chapter_lines = chapter.split('\n') current_chapter_name = chapter_lines[0].split(':')[0] if current_chapter_name == chapter_name: start_index = 1 for line in chapter_lines[1:]: if line: current_question_type = line.split(' ')[0] if current_question_type == question_type: if start_index == question_number: answer_index = line.find('答案:') if answer_index!= -1: return line[answer_index + 3:] # 提取答案部分 start_index += 1 # 根据用户输入判断答案是否正确 def judge_answer(chapter_name, question_type, question_number, user_answer): correct_answer = output_specific_answer(chapter_name, question_type, question_number) if user_answer == correct_answer: return '正确' else: return '错误' # 测试代码 statistics = read_and_statistic_data() for chapter, types in statistics.items(): print(f'{chapter}:') for question_type, count in types.items(): print(f' {question_type} {count} 道') # 示例用法 print(output_specific_question('第二章', '判断题', 6)) print(output_specific_answer('第二章', '判断题', 6)) print(judge_answer('第二章', '判断题', 6, '用户输入的答案')) 注意: 1. 代码中的文件路径需要根据实际情况进行修改。 2. 题目和答案的格式需要与实际文件中的格式相匹配。 3. 对于异常情况的处理,例如文件不存在、用户输入不合法等,还需要进一步完善代码。 ``` ######[AI写代码神器 | 977点数解答 | 2025-01-05 11:24:16]
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)478
- Java实现:C1科目一模拟考试系统,功能全面助力高效备考! (GPT | 555点数解答 | 2024-12-09 10:49:37)272
- Java 实现 C1 科目一模拟考试系统:集题库管理、多样练习与成绩统计于一体 (GPT | 36点数解答 | 2024-12-13 17:35:26)189
- 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
- Python 实现超有趣大鱼吃小鱼游戏,从初始 1 级开启进化之旅!( | 767点数解答 | 2024-05-31 11:50:29)256
- Python 实现移位与仿射密码:精准加密解密及关键逻辑解析(字节豆包 | 798点数解答 | 2024-12-18 15:55:47)269
- Java调用Python接口中文乱码?设置UTF - 8编码一招解决!(讯飞星火 | 263点数解答 | 2024-06-06 17:07:59)406