python答案查询_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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值