python 按行查找文本文件,找出答案,并提示置顶答案

1。整理好答案文件为文本文件;不能有空行;每个题干前有数字做为题号;每个题答案第一个字符为字母,答案占一行import time

import time
import sys
import os
import re
import tkinter
import tkinter.messagebox #这个是消息框,对话框的关键
#sys.path.append(os.path.abspath("SO_site-packages"))
import win32api,win32con
import pyperclip  # 引入模块
#f = open("bbb.txt")
def showMsg(acc): #提示框
    top=tkinter.Tk()
    top.withdraw()
    top.update()
    mes=tkinter.messagebox.showinfo('提示', acc)
    top.mainloop()
    #top.destroy()
    return mes
print("aaa.txt作为答案文件要求:放在当前目录中;不能有空行;每个题占两行,第一行题干,以数字开头;第二行是答案,以字母开头;用鼠标选择,用CTRL+C键盘复制效果好;按ENTER继续")
os.system("pause")
recent_value = ""
tmp_value="" # 初始化(应该也可以没有这一行,感觉意义不大。但是对recent_value的初始化是必须的)
while True:
    tmp_value = pyperclip.paste() 			# 读取剪切板复制的内容
    #print("tmp_value:"+tmp_value)
    print("recent_value:"+recent_value)
    if tmp_value != recent_value:
        #print(tmp_value)#如果检测到剪切板内容有改动,那么就进入文本的修改
        recent_value=tmp_value
        #print(recent_value)
        #f = open("aaa.txt")   #aa.txt作为答案文件要求:不能有空行;每个题占两行,第一行题干,以数字开头;第二行是答案,以字母开头
        with open("aaa.txt","r") as f:
            line = f.readline()             # 调用文件的 readline()方法
            av=""
            while line:
                if len(line.strip())>0:
                    if line.strip()[0].isdigit():
                        a1=line
                        bz=1
                    if line.strip()[0].isalpha():
                        a2=line
                        bz=2
                    if line.find(tmp_value)>-1:
                        if bz==2:
                            av=av+a1+a2
                        if bz==1:
                            line = f.readline()
                            av=av+a1+line
                    #print(line)
                    line=f.readline()
                    continue
        if len(av)!=0:
            top=tkinter.Tk()
            #top.withdraw()#这行可以隐藏主窗口,但又不置顶消息窗口,所以关了
            mes=tkinter.messagebox.showinfo('提示', av)
            top.wm_attributes('-topmost',1)  #置顶消息窗口
            top.destroy()#关闭消息窗口
        #print(tmp_value)
        continue
        #continue
    continue
            
        

  



猜你喜欢

转载自www.cnblogs.com/xkdn/p/12243810.html