以下是用 Python 实现的代码示例: ```python import os def count_question_types(): # 定义文件路径 file_path = "/data/share_files/experiment_file/e_16545/datafiles/python 题库(考试用)" # 检查文件是否存在 if not os.path.exists(file_path): print("文件不存在") return chapter_dict = {} # 用于存储各章节的题目类型和数量 with open(file_path, 'r') as file: current_chapter = None # 当前处理的章节 for line in file.readlines(): if "第" in line and ":" in line: # 如果是章节行 current_chapter = line.split(":")[0] # 获取章节名称 chapter_dict[current_chapter] = {} # 初始化该章节的统计字典 elif current_chapter: # 如果在处理某个章节 # 根据具体题目格式进行判断和统计 # 这里假设题目以"选择题:"、"判断题:"等开头 if "选择题:" in line: if "选择题" in chapter_dict[current_chapter]: chapter_dict[current_chapter]["选择题"] += 1 else: chapter_dict[current_chapter]["选择题"] = 1 elif "判断题:" in line: if "判断题" in chapter_dict[current_chapter]: chapter_dict[current_chapter]["判断题"] += 1 else: chapter_dict[current_chapter]["判断题"] = 1 # 输出统计结果 for chapter, types_count in chapter_dict.items(): print(f"{chapter}:") for type_, count in types_count.items(): print(f"{type_} {count} 道") count_question_types() ``` 额外需要注意的逻辑点: 1. 题目格式可能存在多种情况,需要根据实际情况进行灵活判断和处理。 2. 如果文件过大,考虑使用逐行读取或者分块读取的方式,避免内存占用过高。 3. 处理异常情况,比如文件读取失败、题目格式不符合预期等。 [2024-12-17 11:13:01 | AI写代码神器 | 478点数解答]