扬大又来党校考试了, 迄今我已经考了三次了。
这次用python写了个,题目与答案中正则匹配出来,字符编码为utf-8,颜色打印使用unix终端格式,所以并不能
正常在win下使用,建议用linux, 如果在win下使用会乱码,会是这么个样子,看清答案有困难。
首先有三个文件,py脚本,答案txt,以及考试页面源码html(在登录党校考试后,点击立即考试时按住ctrl键就能在新标签页打开,然后点击chrome菜单里‘’将页面存储为‘’,即得到一个html页面源码和一个用不到的保存了页面js的文件夹)。
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 '%s\n%s\n\033[37;91m%s\033[0m'%(result[j+1],result[j+2],result[j+3])
- return
- else:
- for i in result:
- if string in i:
- print '%d.%s\033[37;91m%s\033[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分钟。
得了满分。
结束了。