有一个朋友,让我把文本文件转化为.csv的形式,在经过多方咨询的情况下,产生了如下的脚本
在此记录一下防止以后还要用:

import openpyxl
import re

from tqdm import tqdm

f = open(r'0.txt', 'r', encoding='utf8').read().splitlines()
quesli = []
ansli = []
choiceli = []
print('analysis')
i = 0
n1=0
n2=0
tmp = ''
while i < len(f):
    if  re.match('(.*)[ABCDE][、..]|(.*)[ABCDE] [、..]',f[i]):
        tmp += f[i]
    else:
        if len(tmp) != 0:
            choiceli.append(tmp)
            n1+=1
            tmp = ''
        temp = f[i].replace('、', '. ').replace('(', '(').replace(')', ')')
        ansli.append(temp.split('(')[-1].split(')')[0].replace(' ', '').replace(' ',''))
        quesli.append(temp.split('(')[0] + '()' + temp.split(')')[1])
        n2+=1
    if abs(n1-n2)>=2:
        print(i)
        exit('error')
    i+=1
choiceli.append(tmp)
def chuli2(x):#把各个选项之间插入换行符,得到选项列。
    return re.sub('[B-Z]',lambda x:"\r\n"+x.group(0),x)
choice=[]
for i in choiceli:
    choice.append(chuli2(i))
print(choice)
excel = openpyxl.Workbook()
sheet = excel.create_sheet()
rowTitle = [u'题目',u'选项',u'答案']
print('dump it')
# 增加title
for i in range(len(rowTitle)):
    sheet.cell(1, i + 1, rowTitle[i])
for i in tqdm(range(len(quesli))):
    rowData=[quesli[i],choice[i],ansli[i]]
    for num in range(len(rowData)):
        sheet.cell(2+i, num + 1, rowData[num])

excel.save('2.csv')