如何将 LaTeX 代码按字面意思存储在 Python 中?
我正在使用 python 和 sqlite3 制作物理解决方案的数据库。我有很多想要存储的 LaTeX 代码,但遇到了问题。许多 LaTeX 代码都包含 python 解释的字符,而不是仅仅接受作为文字文本。例如,我尝试了以下方法来存储 \frac{}{}:
foo = "\frac{}{}"
foo2 = """\frac{}{}"""
foo3 = '''\frac{}{}'''
print foo
rac{}{}
foo
Out[10]: '\x0crac{}{}'
我真的希望它能够逐字存储,就像原始的 ascii 字符一样。有办法做到这一点吗?
I am using python and sqlite3 to make a database of physics solutions. I have a lot of LaTeX code that I would like to store, but I'm running into a problem. A lot of LaTeX code has characters that python interprets instead of just accepting as literal text. For example, I've tried the following to store \frac{}{}:
foo = "\frac{}{}"
foo2 = """\frac{}{}"""
foo3 = '''\frac{}{}'''
print foo
rac{}{}
foo
Out[10]: '\x0crac{}{}'
I would really like it to just be stored verbatim, as the original ascii characters. Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会简单地使用原始字符串:
这将阻止Python解释反斜杠作为转义序列并将按原样包含它们。
I would simply use raw strings:
That'll stop Python from interpreting backslashes as escape sequences and will include them as-is.
尝试通过在字符串前添加
r
来存储它们:Try to store them by prefixing the string with
r
:查看原始字符串。
Have a look at raw strings.
为了完整起见:您也可以编写
'\\frac{}{}'
。或者你可以写一个函数
Just in order to be complete: you as well could write
'\\frac{}{}'
.Or you can write a function