[英]get string from mongodb to python as raw string solved but new problem with parse_latex
i am getting following as string from MongoDB query:我从 MongoDB 查询中得到以下字符串:
1000*\frac{1-{(\frac{1}{1+0.1025})^{10}}}{0.09806}'
like to take it to equate to expression喜欢把它等同于表达
expression = '1000*\frac{1-{(\frac{1}{1+0.1025})^{10}}}{0.09806}'
then convert to sympy expression with然后转换为 sympy 表达式
expression_fin = parse_latex(expression)
but I got errors as this is not a raw string但我收到错误,因为这不是原始字符串
I tried the following:我尝试了以下方法:
expression1=f'{expression}'
expression = repr('1000*\frac{1-{(\frac{1}{1+0.1025})^{10}}}{0.09806}')
both attempts does not works please help if any Edited:两种尝试都不起作用,如果有的话请帮忙编辑:
def step_exec(step_id, part_req):
print('printing here')
newEq=repr(part_req['extras'][0]['fn_e8wgwASbHjuLR7Mb6mH2pC'])
print(newEq)
# rawtest = r'%s' % newEq
rawtest = parse_expr(newEq, evaluate=False)
sympy_inputFrom_parse_expr=parse_latex(rawtest)
print('sympy_inputFrom_parse_expr',sympy_inputFrom_parse_expr)
latex_inputFromSympy=latex(sympy_inputFrom_parse_expr)
print('latex_inputFromSympy',latex_inputFromSympy)
print(latex_inputFromSympy == part_req['extras'][0]['fn_e8wgwASbHjuLR7Mb6mH2pC'])
pprint.pprint(part_req['extras'][0]['fn_e8wgwASbHjuLR7Mb6mH2pC'])
print('raw here')
rawRstring=r'1200*\frac{1-{(\frac{1}{1+0.1259})^{12}}}{0.11917}'
print('rawRstring',rawRstring)
parsLatToSympy = parse_latex(rawRstring)
print('parsLatToSympy',parsLatToSympy)
SympyTOlatex=latex(parsLatToSympy)
print('SympyTOlatex',SympyTOlatex)
print('latex_inputFromSympy', latex_inputFromSympy)
return step_id
the result:结果:
printing here
'1200*\frac{1-{(\frac{1}{1+0.1259})^{12}}}{0.11917}' '1200*\frac{1-{(\frac{1}{1+0.1259})^{12}}}{0.11917}'
sympy_inputFrom_parse_expr 1200*((1 - 1/(0.1259 + 1)**12)/0.11917) sympy_inputFrom_parse_expr 1200*((1 - 1/(0.1259 + 1)**12)/0.11917)
latex_inputFromSympy 1200 \frac{1 - \frac{1}{\left(0.1259 + 1\right)^{12}}}{0.11917} latex_inputFromSympy 1200 \frac{1 - \frac{1}{\left(0.1259 + 1\right)^{12}}}{0.11917}
False错误的
'1200*\frac{1-{(\frac{1}{1+0.1259})^{12}}}{0.11917}' '1200*\frac{1-{(\frac{1}{1+0.1259})^{12}}}{0.11917}'
raw here在这里生
rawRstring 1200*\frac{1-{(\frac{1}{1+0.1259})^{12}}}{0.11917} rawRstring 1200*\frac{1-{(\frac{1}{1+0.1259})^{12}}}{0.11917}
parsLatToSympy 1200*((1 - 1/(0.1259 + 1)**12)/0.11917) parsLatToSympy 1200*((1 - 1/(0.1259 + 1)**12)/0.11917)
SympyTOlatex 1200 \frac{1 - \frac{1}{\left(0.1259 + 1\right)^{12}}}{0.11917} SympyTOlatex 1200 \frac{1 - \frac{1}{\left(0.1259 + 1\right)^{12}}}{0.11917}
latex_inputFromSympy 1200 \frac{1 - \frac{1}{\left(0.1259 + 1\right)^{12}}}{0.11917} latex_inputFromSympy 1200 \frac{1 - \frac{1}{\left(0.1259 + 1\right)^{12}}}{0.11917}
the raw string problem is solved from "parse_expr" but it changes the expression please help if any原始字符串问题从“parse_expr”解决,但它改变了表达式,如果有的话请帮忙
Raw string can be created by prefixing a string literal with r""
.可以通过在字符串文字前加上
r""
来创建原始字符串。 In your case:在你的情况下:
from sympy.parsing.latex import parse_latex
r_string = r"1000*\frac{1-{(\frac{1}{1+0.1025})^{10}}}{0.09806}"
expression = parse_latex(r_string)
expression
EDIT to accommodate comment.编辑以容纳评论。 Yeah, nice problem you have :) For your specific case you cold do this
s.encode('unicode_escape').decode()
.是的,你有很好的问题:) 对于你的具体情况,你冷做这个
s.encode('unicode_escape').decode()
。 But this is likely going to modify the characters following backslashes, so you would also have to perform a replacement to bring them back to the original intent.但这很可能会修改反斜杠后面的字符,因此您还必须执行替换以使它们恢复原始意图。 For example:
例如:
s = "1000*\frac{1-{(\frac{1}{1+0.1025})^{10}}}{0.09806}"
# convert to raw, according to https://stackoverflow.com/a/65380295/2329968
r_string = s.encode('unicode_escape').decode()
# at this point you have a problem: the character inside s following a
# backslash might change to something else. Need to bring it back
r_string_mod = r_string.replace("x0c", "f")
parse_latex(r_string_mod)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.