Python 题库数据处理:统计、查询与答案判断全流程实现
```python import os import codecs # 定义读取文件并处理数据的函数 def process_data(): # 文件夹路径 folder_path = '/data/share_files/experiment_file/e_16545/datafiles' # 文件名称 file_name = 'python 题库(考试用)' # 文件完整路径 file_path = os.path.join(folder_path, file_name) # 以 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) >= 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 in chapter_dict.items(): print(f"{chapter}:") for question_type, count in types.items(): print(f"{question_type}{count}道") # 定义根据输入输出指定题目的函数 def output_question(chapter, question_type, question_num): # 重复读取文件的代码 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.read() lines = data.split('\n') found = False for line in lines: # 按照格式找到指定题目 parts = line.split(' ') if len(parts) >= 3 and parts[0] == chapter and parts[1] == question_type and int(parts[2]) == question_num: print(line[len(chapter) + len(question_type) + 2:]) # 输出题目内容 found = True break if not found: print("未找到指定题目") # 定义根据输入输出指定题目答案的函数 def output_answer(chapter, question_type, question_num): # 重复读取文件的代码 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.read() lines = data.split('\n') found = False for line in lines: # 按照格式找到指定题目及答案 parts = line.split(' ') if len(parts) >= 4 and parts[0] == chapter and parts[1] == question_type and int(parts[2]) == question_num: print(parts[3]) # 输出答案 found = True break if not found: print("未找到指定题目的答案") # 定义判断用户答案是否正确的函数 def judge_answer(chapter, question_type, question_num, 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.read() lines = data.split('\n') found = False correct_answer = None for line in lines: # 按照格式找到指定题目及答案 parts = line.split(' ') if len(parts) >= 4 and parts[0] == chapter and parts[1] == question_type and int(parts[2]) == question_num: correct_answer = parts[3] found = True break if found: if user_answer == correct_answer: print("正确") else: print("错误") else: print("未找到指定题目") if __name__ == "__main__": process_data() ``` 1. 代码中假设了题目文件的格式,实际使用时需确保格式符合预期。 2. 处理文件路径时,要注意路径的正确性,确保能正确找到题库文件。 3. 对于用户输入的内容,需要进行适当的校验和错误处理,以避免程序出错。 ######[AI写代码神器 | 1128点数解答 | 2024-12-21 11:45:52]
- 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 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- 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
- 工程师必知!Monkey 流程助力软件稳定性与可靠性提升(百度文心 | 388点数解答 | 2023-11-09 18:28:07)283
- 工程师揭秘:重要 APP 从需求到上线全流程攻略(百度文心 | 334点数解答 | 2023-11-09 18:31:47)282
- 补全含错误代码:Vue结合Element UI组件实现查询、新增及表格展示功能 ( | 400点数解答 | 2023-12-19 15:20:50)432
- 单线程频繁查ClickHouse致CPU飙升?这些优化方案速看!(DeepSeek | 491点数解答 | 2025-02-13 17:49:54)227
- MySQL:巧用 NOT LIKE 查询字段不包含特定字符串及注意要点 (DeepSeek | 264点数解答 | 2025-02-13 18:25:01)178