从乳胶文件中提取数字 [英] Extract figures from latex file
本文介绍了从乳胶文件中提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以用一只手来解决以下问题.我正在尝试编写一个 python 脚本,该脚本将从 tex 文件中提取数字并将它们放入另一个文件中.输入文件是这样的:
Hi I could use a hand with the following problem. I'm trying to write a python script that would extract the figures from a tex file and put them into another file. The input file is something like this:
\documentclass[]....
\begin{document}
% More text
\begin{figure}
figure_info 1
\end{figure}
\begin{figure}
figure_info 2
\end{figure}
%More text
输出文件应该是这样的:
And the output file should be something like this:
\begin{figure}
figure_info 1
\end{figure}
\begin{figure}
figure_info 2
\end{figure}
感谢您的帮助.
推荐答案
非常感谢您的回答,我终于以这种方式完成了.这可能不是最佳方式,但它有效.我尝试了几种建议的解决方案,但它们需要进行一些调整才能使其发挥作用.
Thanks a lot for the answers I've finally done it this way. It probably isn't the optimal way but it works. I tried several of the proposed solutions but they need some tweaking to get them to work.
infile = open('data.tex', 'r')
outfile = open('result.tex', 'w')
extract_block = False
for line in infile:
if 'begin{figure}' in line:
extract_block = True
if extract_block:
outfile.write(line)
if 'end{figure}' in line:
extract_block = False
outfile.write("------------------------------------------\n\n")
infile.close()
outfile.close()
这篇关于从乳胶文件中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文