错题收集器

最近在准备一个考试,为了做题模拟真正考试,培训班老师建议我们把试题打印之后做题。那就有一个问题,怎么收集错题?一个一个copy对程序员来说是不能忍受的,我就尝试用python写了个简单的小脚本。
想法很简单,需要输入试题文件,答案文件,错题序列号,时间有限,我只支持txt..文件格式统一,特别是题的序号,比如1.2.3.(要不然都考试完了,我估计还没写出这个脚本),会输出相应的错题+答案
python脚本如下:

import os
import sys
import re
import tkinter as tk  # 使用Tkinter前需要先导入

file_path_org="/shiti"
file_path_org_answer="/daan"
file_path_error = "/input.txt"
file_path_question_out="/"


def read_org(number, sepStr, file_path):
    
    with open(file_path, mode="r") as f:
        read_s = f.read()
        read_s = read_s.replace('. 解析', '.解析') 

        # sepStr = "、"
        # sepStr = '.解析'
        # print(number+sepStr)
        index = read_s.index(number+sepStr)
        number1 = int(number)+1
        number1Str = str(number1)
        if number=='200':
            formatStr = read_s[index:].replace('201','')
            return formatStr
        else:
            index1 = read_s.index(number1Str+sepStr)
            if index1 > index:
                # print(read_s[index:index1])
                formatStr = read_s[index:index1]
            
                return formatStr


def read_orign() :
    with open(getpwd() + file_path_error, mode="r") as f:
        read_s = f.readlines()
        # print(read_s)
        return read_s

def write_question(listStr, out_path):
    with open(out_path, mode="w") as f:
        f.write(listStr)

def init_window():
    top = tk.Tk()
    top.title('错题生成器')
    top.geometry('500x200')
    top.resizable()
    # 进入消息循环
    l0 = tk.Label(top, text='选择需要生成的错题:').pack(side='left')
    var = tk.StringVar()
    global lb
    lb = tk.Listbox(top, listvariable=var)
    # 放入列表的值
    list_item = [1, 2, 3, 4,5,6,7,8]  # 控件的内容为1 2 3 4
    for item in list_item:
        lb.insert('end', item)

    # 绑定事件函数
    lb.bind('', print_item)
    lb.pack()
    top.mainloop()

def getpwd():
    pwd = os.path.dirname(os.path.realpath(sys.argv[0]))
    # if os.path.isfile(pwd):
    #     pwd = os.path.dirname(pwd)
    return pwd

def print_item(event):
    res = lb.get(lb.curselection())

    orglist = read_orign()
    list1 = []
    for i in orglist:
        num = i[:-1]
        # 34是、2是.
        shiti_path = getpwd() + file_path_org+str(res)+'.txt'
        daan_path = getpwd() + file_path_org_answer+str(res)+'.txt'
        sep='.'
        sep1='.解析'
        if res==3 or res==4:
            sep='、'
        list1.append(read_org(num, sep, shiti_path))
        list1.append(read_org(num, sep1, daan_path))

    listStr = "\n ------------------------------------------------------------------------- \n ".join('%s' %id for id in list1)
    out_path= getpwd() + file_path_question_out+'PMP 复习测试题'+str(res)+'.doc'
    write_question(listStr,out_path)    

init_window()

为了提供给其他小伙伴,考虑到他们没有python环境,我用pyinstaller打包成可执行文件。

你可能感兴趣的:(错题收集器)