def get_user_info_from_txt(path=None, local_path='local_file'): user_info = [] if None == path: user_name_path = 'user_name.txt' else: user_name_path = path ''' with open(local_path,'rt',encoding='UTF-8') as file: last_md5 = re.sub('\n', '', file.readline()) last_pos = re.sub('\n', '', file.readline()) file.close() ''' #获取上次答题时的md5校验码以及答题位置 data = easy_read.read_from_utf8(local_path) if None == data: last_pos = -1 last_md5 = 0 else: lines = data.split('\n') last_md5 = lines[0] if (len(lines) == 2): last_pos = lines[1] else: last_pos = -1 #计算本次答题的md5校验码 new_md5 = calc_md5(user_name_path) if last_md5 != new_md5: last_pos = -1 print('file change, do from the start!') with open(local_path, 'w') as file: file.write(new_md5) file.close() data = easy_read.read_from_utf8(user_name_path) lines = data.split('\n') for line in lines: user_info.append([line, 'Aa' + line]) if '' == last_pos: last_pos = -1 print(last_pos) return user_info, int(last_pos)
def record_pos_2_local(pos, local_path='local_file'): data = easy_read.read_from_utf8(local_path) lines = data.split('\n') md5 = lines[0] with open(local_path, 'w', encoding='UTF-8') as file: file.write(md5) file.write('\n') file.write(str(pos)) file.close()
def __init_user_info(self): #从文件读取 data = easy_read.read_from_utf8(self.login_record_file_path) if data is None: last_pos = -1 last_md5 = 0 else: #local_file文件格式:第一行记录文件md5值,第二行记录上次答题最后答题用户的位置 lines = data.split('\n') last_md5 = lines[0] if len(lines) == 2: last_pos = int(lines[1]) else: last_pos = -1 #计算当前user_info文件的md5值 new_md5 = self.calc_md5(self.user_info_file_path) self.md5 = new_md5 #新旧user_info不同,为了保证将所有用户都答题,讲user_info中的用户全部重新答 if last_md5 != new_md5: self.pos = -1 log.success_log('new', '') log.error_log('', 'new') else: self.pos = last_pos data = easy_read.read_from_utf8(self.user_info_file_path) lines = data.split('\n') for line in lines: #跳过空行 if len(line) == 0: continue username, password = line.split(' ') self.user_info.append([username, password])
def question_bank_init(self): data = easy_read.read_from_utf8(self.file_path) data_list = data.split('\n') self.total_question = int(len(data_list) / 6) for prob_index in range(self.total_question): prob = '' ans = [] option = [] for prob_desc_index in range(6): index = prob_index * 6 + prob_desc_index line = data_list[index] if 0 == prob_desc_index: index = line.find('】') if -1 != index: prob = line[index + 1:] prob = self.unpunctuate(prob) elif 5 == prob_desc_index: index = line.find(':') if -1 != index: #二月份题目答案没有用','隔开 ans = line[index + 2:].split(',') #ans = list(line[index+1:]) else: index = line.find('、') if -1 != index: option.append( question_bank.unpunctuate(line[index + 1:])) #将题目内容补全,用于模糊查找 prob_desc_list = list(prob) npos = prob_desc_list.index('(') ans_str = ''.join(ans) if -1 != ans_str.find('D'): prob_desc_list.insert(npos + 1, option[3]) if -1 != ans_str.find('C'): prob_desc_list.insert(npos + 1, option[2]) if -1 != ans_str.find('B'): prob_desc_list.insert(npos + 1, option[1]) if -1 != ans_str.find('A'): prob_desc_list.insert(npos + 1, option[0]) prob_desc_str = ''.join(prob_desc_list) self.prob_desc.append(prob_desc_str) self.prob_set[prob] = [option, ans]
import auto_exam import easy_read import re data = easy_read.read_from_utf8('prob_set') prob_test = '我们将继续坚持以经济建设为中心致力于建设改革发展成果真正惠及人民()生态文明全面发展的小康社会' test_opt_a = '经济' test_opt_b = '政治' test_opt_c = '文化' test_opt_d = '社会' test_ans = ['A','B','C','D'] prob_all = [] data_list = data.split('\n') total_question = int(len(data_list)/6) for prob_index in range(total_question): prob = '' prob_desc = data_list[prob_index * 6] index = prob_desc.find('】') if -1 != index: prob_desc = prob_desc[index + 1:] opt_a = data_list[prob_index * 6 + 1] opt_b = data_list[prob_index * 6 + 2] opt_c = data_list[prob_index * 6 + 3] opt_d = data_list[prob_index * 6 + 4] ans = data_list[prob_index * 6 + 5]