Python|python实现将题目转化为字典!

本文介绍了如何使用Python的docx库读取Word文档中的题目信息,并通过正则表达式处理,将其转化为JSON格式的字典结构。具体步骤包括读取文档、提取信息、匹配选项及答案、构建字典并存储输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述 

在这里首先要提到 JSON 文件, JSON 文件是用来存储简单的数据结构和对象的文件,可以在web 应用程序中进行数据交换。而它的格式就有点类似于常用的字典结构,形如: {‘title’ :’ 关于《花间集》说法错误的是 ’ ,’content’ :{ ‘A’ :’ 作者是赵崇佐 ’, ’B’ : ‘ 收录当时流行歌曲歌词 ’ }, ‘true_choice’:”C” , ’type’:’ 单选题 ’  } 。今天要做的就是读取 word 里的信息并把它们按照如上的格式进行转化。

解决方案 

首先要用 python 来解决并处理 word 的文档,就需要引进 docx 的库来读取 word 里的信息,读取出信息后,可以用正则表达式对信息进行进一步的提取和处理,最后以字典的格式存储并输出。

第一步引用 docx 库,读取每一个题目的信息并按不同的题目存放在列表中方便下一步处理。

file = docx.Document(s) 
all_paragraphs = file.paragraphs 
paragraphs_text = [] 
for paragraph in all_paragraphs: 
paragraphs_text.append(paragraph.text) 
l = [] 
a = 0 
for i in range(len(paragraphs_text)): 
if paragraphs_text[i] == '': 
l.append(paragraphs_text[a:i]) 
a = i

第二步用正则表达式对信息进行进一步的提取和处理,最后字典的格式存储并输出。

list = [] 
for questions in l: 
val = {} 
cotent = {} 
for strs in questions: 
if re.match('\d', strs): 
val['title'] = strs 
if re.match('A', strs): 
cotent['A'] = strs[2:] 
if re.match('B', strs): 
cotent['B'] = strs[2:] 
if re.match('C', strs): 
cotent['C'] = strs[2:] 
if re.match('D', strs): 
cotent['D'] = strs[2:] 
if re.match(' 答案: ', strs): 
val['true_choice'] = strs[3:] 
if re.match(' 题型: ', strs): 
val['type'] = strs[3:] 
if len(cotent) > 1: 
val['count'] = cotent 
list.append(val) 
return list

完整代码如下:

import docx 
import re 
def f(s): 
file = docx.Document(s) 
all_paragraphs = file.paragraphs 
paragraphs_text = [] 
for paragraph in all_paragraphs: 
paragraphs_text.append(paragraph.text) 
l = [] 
a = 0 
for i in range(len(paragraphs_text)): 
if paragraphs_text[i] == '': 
l.append(paragraphs_text[a:i]) 
a = i 
list = [] 
for questions in l: 
val = {} 
cotent = {} 
for strs in questions: 
if re.match('\d', strs): 
val['title'] = strs 
if re.match('A', strs): 
cotent['A'] = strs[2:] 
if re.match('B', strs): 
cotent['B'] = strs[2:] 
if re.match('C', strs): 
cotent['C'] = strs[2:] 
if re.match('D', strs): 
cotent['D'] = strs[2:] 
if re.match(' 答案: ', strs): 
val['true_choice'] =  strs[3:] 
if re.match(' 题型: ', strs): 
val['type'] = strs[3:] 
if len(cotent) > 1: 
val['count'] = cotent 
list.append(val) 
return list 

print(f("D://print2.docx"))

效果展示:

输出:

END

### Python 字典练习题与例题 以下是几个关于 Python 字典的经典练习题和例题,帮助巩固基础知识并提升编程能力。 #### 合并两个字典 编写一个函数 `merge_dicts` 来合并两个字典。如果存在相同的键,则保留第二个字典中的值。 ```python def merge_dicts(dict1, dict2): result = dict1.copy() result.update(dict2) return result dict_a = {"a": 1, "b": 2} dict_b = {"b": 3, "c": 4} merged_dict = merge_dicts(dict_a, dict_b) print(merged_dict) # 输出 {'a': 1, 'b': 3, 'c': 4} ``` 此实现基于内置方法 `.update()` 的特性[^2]。 --- #### 计算字符频率 给定一段字符串,统计其中每个字母出现的次数,并返回一个字典形式的结果。 ```python def char_frequency(s): freq = {} for char in s.lower(): if char.isalpha(): # 只考虑字母 freq[char] = freq.get(char, 0) + 1 return freq text = "Hello World" result = char_frequency(text) print(result) # 输出 {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1} ``` 该算法通过遍历字符串并将结果存储到字典中完成任务[^1]。 --- #### 找出字典中最常见的键 创建一个函数来找出字典中具有最大值的键。 ```python def most_common_key(d): if not d: return None max_key = max(d, key=d.get) return max_key data = {"apple": 5, "banana": 8, "cherry": 3} common_key = most_common_key(data) print(common_key) # 输出 banana ``` 这里利用了 `max()` 函数配合自定义比较逻辑找到目标键[^3]。 --- #### 将列表转换为嵌套字典 假设有一个二维列表表示父子关系的数据结构,将其转化为嵌套字典。 ```python def list_to_nested_dict(lst): nested_dict = {} for parent, child in lst: if parent not in nested_dict: nested_dict[parent] = [] nested_dict[parent].append(child) return nested_dict pairs = [("A", "B"), ("A", "C"), ("B", "D")] nested_result = list_to_nested_dict(pairs) print(nested_result) # 输出 {'A': ['B', 'C'], 'B': ['D']} ``` 这段代码展示了如何处理复杂数据结构之间的映射关系[^1]。 --- #### 判断两字典是否相等(忽略顺序) 设计一种方式判断两个字典的内容是否完全一致,即使它们内部项的排列不同也不影响判定结果。 ```python from collections import Counter def are_equal_ignore_order(dict1, dict2): return Counter(dict1) == Counter(dict2) test1 = {"x": 1, "y": 2} test2 = {"y": 2, "x": 1} are_same = are_equal_ignore_order(test1, test2) print(are_same) # 输出 True ``` 借助标准库模块 `collections.Counter` 实现高效对比操作[^2]. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值