You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

68 lines
2.7 KiB

import csv
csv_file = open("output.csv", mode="w", encoding="UTF-8", newline='')
writer = csv.writer(csv_file)
src_file = open("drone.txt", mode="r", encoding="UTF-8")
src_string = src_file.read()
chap1_pos = src_string.find("第一章")
chap2_pos = src_string.find("第二章", chap1_pos)
chap3_pos = src_string.find("第三章", chap2_pos)
chap4_pos = src_string.find("第四章", chap3_pos)
chap1_ans_pos = src_string.find("第一章", chap4_pos)
chap2_ans_pos = src_string.find("第二章", chap1_ans_pos)
chap3_ans_pos = src_string.find("第三章", chap2_ans_pos)
chap4_ans_pos = src_string.find("第四章", chap3_ans_pos)
data_list = []
def getBlock(current_pos, end_pos, current_ans_pos, end_ans_pos, label):
question_number = 1
while(1):
search_key = str(question_number) + ". "
question_pos = src_string.find(search_key, current_pos, end_pos)
if(question_pos == -1):
break
#print(src_string[question_pos:].split("\n")[0])
options1_pos = src_string.find("(A)", question_pos, end_pos)
options2_pos = src_string.find("(B)", options1_pos, end_pos)
options3_pos = src_string.find("(C)", options2_pos, end_pos)
options4_pos = src_string.find("(D)", options3_pos, end_pos)
question_str = src_string[question_pos:].split("\n")[0]
options1_str = src_string[options1_pos+4 : options2_pos]
options2_str = src_string[options2_pos+4 : options3_pos]
options3_str = src_string[options3_pos+4 : options4_pos]
options4_str = src_string[options4_pos+4:].split("\n")[0]
#print(question_str)
question = []
question.append(question_number)
question.append(question_str.split(search_key)[1])
#question.append(options1_str.split("(A)")[1])
#question.append(options2_str.split("(B)")[1])
#question.append(options3_str.split("(C)")[1])
#question.append(options4_str.split("(D)")[1])
question.append(options1_str.strip())
question.append(options2_str.strip())
question.append(options3_str.strip())
question.append(options4_str.strip())
ans_pos = src_string.find(search_key, current_ans_pos, end_ans_pos)
ans_str = src_string[ans_pos:].split(search_key)[1][0]
question.append(ans_str)
question.append(label+str(question_number))
data_list.append(question)
#print(question)
question_number += 1
getBlock(chap1_pos, chap2_pos, chap1_ans_pos, chap2_ans_pos, "第一章")
getBlock(chap2_pos, chap3_pos, chap2_ans_pos, chap3_ans_pos, "第二章")
getBlock(chap3_pos, chap4_pos, chap3_ans_pos, chap4_ans_pos, "第三章")
getBlock(chap4_pos, chap1_ans_pos, chap4_ans_pos, -1, "第四章")
writer.writerows(data_list)