通过使用正则表达式格式化文本

时间:2016-05-29 09:17:17

标签: regex latex

我不擅长正则表达式,我非常感谢这些领域的专家提供的帮助。

我有一份文件如下:

Text of the question 1.

A. answer A

B. answer B

C. answer C

D. answer D

E. answer E

Good answers: A, C, E

Text of the question 2.

A. ...

我希望将其格式化为(我猜是使用正则表达式):

\question Text of the question 1.

\begin{itemize}

 \item answer A

 \item answer B

 \item answer C

 \item answer D

 \item answer E

\end{itemize}

\begin{solution}

Good answers: A, C, E

\end{solution}


\question Text of the question 2.

\begin{itemize}

 \item ...

\end{itemize}

我可以A.替换\begin{itemize}\n \itemGood answers替换\begin{solution}\nGood answers:但是我对正则表达式的了解不允许我添加{{1} },\question标记,

1 个答案:

答案 0 :(得分:1)

看一下例子。

old_line = ""
with open("in.txt", "r") as f, open("out.txt", "w") as h:
     for ind,l in enumerate(f):
        l = l.strip()
        line = l.lower()
        if (len(line) < 2): continue
        if line.startswith("a."):  
            if(ind > 2): h.write("\\end{solution}\n")
            h.write("\question {0}\n".format(old_line))
            h.write("\\begin{itemize}\n")
        if line.startswith("good answers: "):  h.write("\\end{itemize}\n\\begin{solution}\n"+l+"\n")

        if line[0] > 'a' and line[0] <= 'z' and line[1] == '.':  h.write("\item {0}\n".format(l))
        old_line = l

     h.write("\\end{solution}\n")

在:

Text of the question 1.

A. answer A

B. answer B

C. answer C

D. answer D

E. answer E

Good answers: A, C, E

输出:

\question Text of the question 1.
\begin{itemize}
\item B. answer B
\item C. answer C
\item D. answer D
\item E. answer E
\end{itemize}
\begin{solution}
Good answers: A, C, E
\end{solution}