From 4230523b6c5dc5a77f49064fdcd2fedbe65a71c0 Mon Sep 17 00:00:00 2001 From: 965 Date: Sat, 3 Aug 2024 13:17:46 +0800 Subject: [PATCH] Update Tools --- iSmartAnsAnalize/scripts.py | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 iSmartAnsAnalize/scripts.py diff --git a/iSmartAnsAnalize/scripts.py b/iSmartAnsAnalize/scripts.py new file mode 100644 index 0000000..31acc48 --- /dev/null +++ b/iSmartAnsAnalize/scripts.py @@ -0,0 +1,53 @@ +# encoding utf-8 +import linecache +import re +import os + + +def opentxt(num, path, name, line): + filename = '.\\{0}\\{1}{2}.txt'.format(path, num, name) + cont = linecache.getline(filename, line) + return cont + + +def savetxt(num, path, inv, name, character): + filename = '.\\{0}\\{1}{2}.txt'.format(path, num, name) + with open(filename, 'w', encoding='UTF-8') as txt: + txt.write(inv.replace(character, '\n')) + + +def txtsearch(num, path, character): + filename = '.\\{0}\\{1}_Response.txt'.format(path, num) + txt = open(filename, 'r', encoding='UTF-8') + lines = txt.readlines() + for lines in lines: # 对TXT 进行逐行读取 + if character in lines: + return lines + txt.close() + + +count = 2 +while count < 26: + content = txtsearch(count, 'response', 'answer') + texts = re.findall(r'.*<\\/answers>', content) + texts = str(texts) + texts = re.findall(r'CDATA\[.]', texts) + texts = str(texts) + texts = texts.replace("'CDATA[]]'", '\n') + texts = texts.replace("CDATA", '') + savetxt(count, 'Ans', texts, 'ans', ',') + count += 1 + +os.chdir(r"C:/Users/965/Builds/iSmartAnsCatch/Ans") +filedir = "C:/Users/965/Builds/iSmartAnsCatch/Ans" +f = open('../Ans.txt', 'w') +# 遍历文件名 +for i in range(2, 26): + filename1 = str(i) + 'ans.txt' + if i > 0: + filepath = filedir + '/' + filename1 + # 遍历单个文件 读取行数 + for line1 in open(filepath, encoding='UTF-8'): + f.writelines(str(i - 1) + line1) + f.write('\n') +f.close()