Python 题库数据处理:统计、查询、判题全流程实现
```python import os import codecs # 定义函数用于读取数据并处理 def read_and_process_data(): # 文件路径 file_path = '/data/share_files/experiment_file/e_16545/datafiles/python题库(考试用)' # 以 GBK 编码方式打开文件 with codecs.open(file_path, 'r', 'gbk') as file: data = file.read() # 在此处进行数据处理,统计题目类型和数量 # 假设数据格式为:"第二章 选择题 题目内容" 这样的形式 chapter_dict = {} lines = data.split('\n') for line in lines: parts = line.split(' ') if len(parts) >= 3: 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 in chapter_dict.items(): print(f"{chapter}:") for question_type, count in types.items(): print(f"{question_type} {count} 道") # 定义函数用于根据用户输入输出指定题目 def output_specific_question(chapter, question_type, question_number): # 文件路径 file_path = '/data/share_files/experiment_file/e_16545/datafiles/python题库(考试用)' # 以 GBK 编码方式打开文件 with codecs.open(file_path, 'r', 'gbk') as file: data = file.read() # 在此处根据用户输入查找并输出指定题目 # 假设数据格式为:"第二章 选择题 题目内容" 这样的形式 lines = data.split('\n') for line in lines: parts = line.split(' ') if len(parts) >= 3 and parts[0] == chapter and parts[1] == question_type: question_number_found = int(parts[2].split('.')[0]) if question_number_found == question_number: print(line[len(chapter +' '+ question_type +' '):]) break # 定义函数用于根据用户输入输出指定题目答案 def output_specific_answer(chapter, question_type, question_number): # 文件路径 file_path = '/data/share_files/experiment_file/e_16545/datafiles/python题库(考试用)' # 以 GBK 编码方式打开文件 with codecs.open(file_path, 'r', 'gbk') as file: data = file.read() # 在此处根据用户输入查找并输出指定题目答案 # 假设数据格式为:"第二章 选择题 题目内容 答案" 这样的形式 lines = data.split('\n') for line in lines: parts = line.split(' ') if len(parts) >= 4 and parts[0] == chapter and parts[1] == question_type: question_number_found = int(parts[2].split('.')[0]) if question_number_found == question_number: print(parts[-1]) break # 定义函数用于根据用户输入判断答案 def judge_answer(chapter, question_type, question_number, user_answer): # 文件路径 file_path = '/data/share_files/experiment_file/e_16545/datafiles/python题库(考试用)' # 以 GBK 编码方式打开文件 with codecs.open(file_path, 'r', 'gbk') as file: data = file.read() # 在此处根据用户输入查找题目答案并与用户答案进行比较 # 假设数据格式为:"第二章 选择题 题目内容 答案" 这样的形式 lines = data.split('\n') for line in lines: parts = line.split(' ') if len(parts) >= 4 and parts[0] == chapter and parts[1] == question_type: question_number_found = int(parts[2].split('.')[0]) if question_number_found == question_number: correct_answer = parts[-1] if user_answer == correct_answer: print("正确") else: print("错误") break # 调用函数进行测试 read_and_process_data() ``` 1. 注意文件路径的正确性,确保能准确找到题库文件。 2. 假设的数据格式要与实际文件中的数据格式相匹配,否则处理可能出错。 3. 对于用户输入的内容,要进行必要的有效性检查和错误处理。 ######[AI写代码神器 | 1088点数解答 | 2024-12-22 10:18:05]
- 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
- 工程师必知!Monkey 流程助力软件稳定性与可靠性提升(百度文心 | 388点数解答 | 2023-11-09 18:28:07)293
- 工程师揭秘:重要 APP 从需求到上线全流程攻略(百度文心 | 334点数解答 | 2023-11-09 18:31:47)295
- 补全含错误代码: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