python中二进制转化为十进制_Python实现将题目转化为字典!

问题描述

在这里首先要提到 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"))

效果展示:

v2-3552f940c956f8d56797060576fef359_b.jpg

输出:

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值