使用python脚本将(多个)TeX环境提取到csv

2024-06-14 03:18:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中的第一个问题,请客气一点:)

问题

如何将.tex文件(如下所述)提取为平面csv(格式如下)

上下文

我用乳胶制作了所有的MCQ。然而,考试现在100%在线。。。(叹气)

我的LaTeX文件具有以下基本格式

\documentclass[12pt]{exam}
​
\begin{document}

\begin{questions}
 \question What is the answer ? 
 \begin{oneparchoices}
 \choice 75
 \choice 80
 \CorrectChoice 85
 \choice None of the above
 \end{oneparchoices}
\end{questions}
\end{document}

我现在需要提供一个csv,上面的MCQ问题将显示为(Incorrect,Correct,只是为了清楚起见😃

question,answer1,Cor/Inc,answer2,Cor/Inc,answer3,Cor/Inc ,answer4,Cor/Inc,answer5,Cor/Inc

它会变成

What is the answer ?,Inc,70,Inc,75,Inc,80,Inc,85,Cor,None of the above,Inc

每一行显然都是一个新问题

到目前为止可以对应的是什么

由于Extract figures from latex file,我看到了\begin\end之间的环境原则

infile = open('MCQ.tex', 'r')
outfile = open('FlattenMCQ.csv', 'w')
extract_block = False
for line in infile:
    if 'begin{questions}' in line:
        extract_block = True
    if extract_block:
        outfile.write(line)
    if 'end{questions}' in line:
        extract_block = False
        outfile.write("------------------------------------------\n\n")

infile.close()
outfile.close()

我被困的地方 首先测试\begin{questions}然后\question然后\begin{oneparchoices}然后\choice\CorrectChoice的递归性


Tags: csvthelineextractblockoutfileincend