gpt4 book ai didi

python - 在 % 之后删除文本文件中的部分行,除非在 python 中前缀为\

转载 作者:行者123 更新时间:2023-11-28 21:23:54 26 4
gpt4 key购买 nike

我正在尝试从文件中提取 latex 代码,但我不想要注释; (注释以 % 开头)。注释一直到行尾,但我不想删除文字 %(在 \% 中以 \ 开头)。我该怎么做?理想情况下:

   Lamport and has become the dominant method for using \TeX; few
people write in plain \TeX{} anymore. The current version is
\LaTeXe. % this is a comment

% This is a comment; it will not be shown in the final output.
% The following shows a little of the typesetting power of LaTeX:
\begin{align}
E &= mc^2 \\
m &= \frac{m_0}{\sqrt{1-\frac{v^2}{c^2}}}
\end{align}
this is a \% literal symbol.

我会得到:

   Lamport and has become the dominant method for using \TeX; few
people write in plain \TeX{} anymore. The current version is
\LaTeXe.


\begin{align}
E &= mc^2 \\
m &= \frac{m_0}{\sqrt{1-\frac{v^2}{c^2}}}
\end{align}
this is a \% literal symbol.

有没有办法用 Python 做到这一点?

在工作解决方案后进行编辑,感谢大家。

   r'(.*)(?<!\\\)%.*'

最佳答案

您可以对 (?<!\\)%.* 进行正则表达式替换,但这很脆弱,例如\verb!%!可能不是评论。

关于python - 在 % 之后删除文本文件中的部分行,除非在 python 中前缀为\,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16808970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com