加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

党校考试--正则匹配答案-YZU

发布时间:2020-12-14 01:33:44 所属栏目:百科 来源:网络整理
导读:扬大又来党校考试了, 迄今我已经考了三次了。 这次用python写了个,题目与答案中正则匹配出来,字符编码为utf-8,颜色打印使用unix终端格式,所以并不能 正常在win下使用,建议用linux, 如果在win下使用会乱码,会是这么个样子,看清答案有困难。 首先有三

扬大又来党校考试了, 迄今我已经考了三次了。

这次用python写了个,题目与答案中正则匹配出来,字符编码为utf-8,颜色打印使用unix终端格式,所以并不能

正常在win下使用,建议用linux, 如果在win下使用会乱码,会是这么个样子,看清答案有困难。


首先有三个文件,py脚本,答案txt,以及考试页面源码html(在登录党校考试后,点击立即考试时按住ctrl键就能在新标签页打开,然后点击chrome菜单里‘’将页面存储为‘’,即得到一个html页面源码和一个用不到的保存了页面js的文件夹)。


准备好的三个文件,答案txt,页面html与py脚本


py代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import re
import sys

logo = '''
           +-------------------------------------+
           |                                     +
           |        Author: chenjie              +
           |                                     +
           |        Date:   2014/10/13           +
           |                       for dxks.     +
           +-------------------------------------+
       '''

with  open( sys.argv[1] )  as fp: 
	result = [re.match('d{1,3}.(.*)',i).group(1) if re.match('d{1,i) else i.strip() for i in fp ] 
rlen = len(result)

ques_now_count = 0
def givemeanswer( string,aflag ):
	global ques_now_count
	ques_now_count += 1
	if aflag:
		for j in xrange(rlen):
			if string in result[j]:
				print '%d.%s'%(ques_now_count,string)
				print '%sn%sn33[37;91m%s33[0m'%(result[j+1],result[j+2],result[j+3])
				return
	else:
		for i in result:
			if string in i: 
				print '%d.%s33[37;91m%s33[0m'%(ques_now_count,i[:-4],i[-4:]) 
				return
	print 'Not Found:',ques_now_count,'.',string

def main(): 
	print logo
	with  open( sys.argv[2] )  as fp: 
		result = [i for i in re.findall( 'd{1,3}.(.*?。)',fp.read() )] 
	if 80 == len(result):
		print '[+] Found question: 80,seems good.'
	else:
		print '[-] Something wrong... question number should be 80 but now is',len(result)
	print 'Input Enter to contiune...'
	raw_input()
	print '判断题:'
	for i in result[:20]:
		givemeanswer(i,0)#i[5:-8]
	print '选择题:'
	for i in result[20:]:#i[5:-8]
		givemeanswer(i,1)


if  __name__ == '__main__':
	main()


自己对18行的这句

result = [re.match('d{1,i) else i.strip() for i in fp ] 

觉得不错,挺pythonic。


运行:

python '/root/Desktop/党校考试/dxks_extract.py' '/root/Desktop/党校考试/dxks答案.txt' '/root/Desktop/党校考试/CakePHP the rapid development php framework Exams.html'

结果:

将选项填完。

结束后剩余9分钟。

得了满分。


结束了。



(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读