[英]for loop to write multiple files as latex output in python
Hey I'm newbie learning python and LaTeX for an economics course and i have query about how to do a for loop to write LaTex ouput in multiple tex files without repeating codes. 嘿,我是新手,学习经济学课程的python和LaTeX,而且我有关于如何执行for循环以在多个tex文件中编写LaTex输出而不重复代码的查询。
result= ['res','res1','res2','res3']
for r in result:
f = open(r +'.tex', 'w')
latex= r.summary().as_latex()
f.write(latex)
f.close()'
The above code gives an AttributeError: 'str' object has no attribute 'summary' . 上面的代码给出了AttributeError:'str'对象没有属性'summary' 。
The results list contains summary of a regressions that I had run. 结果列表包含我运行的回归的摘要。 So each res is a summary of an OLS regression.
因此,每个res都是OLS回归的摘要。 I want to convert the summary as LATeX code through a for loop instead of writing it like
我想通过for循环将摘要转换为LATeX代码,而不是像这样写
f = open('res.tex', 'w')
f.write(results.summary().as_latex())
f.close()
f = open('res1.tex', 'w')
f.write(res2.summary().as_latex())
f.close()
and so on... Is there any way to do it through a for loop? 依此类推...有没有办法通过for循环来做到这一点?
You say "The results list contains summary of a regressions that I had run" however your code 您说“结果列表包含我运行的回归的摘要”,但是您的代码
result= ['res','res1','res2','res3']
clearly has result
as a list of strings. 显然将
result
作为字符串列表。 This gives your error : " AttributeError: 'str' object has no attribute 'summary'." 这给出了您的错误: “ AttributeError:'str'对象没有属性'summary'。”
If you have four variables, then 如果您有四个变量,那么
result = [res, res1, res2, res3]
would give you a list perhaps of your regressions. 可能会给您列出您的回归分析。
It appears you want to walk over a list of results, and a list of filenames. 您似乎想要遍历结果列表和文件名列表。
Something like 就像是
result = [res, res1, res2, res3]
filename = ['res', 'res1', 'res2', 'res3']
which you can do like this 你可以这样
for (res, name) in zip(result, filename):
f = open(name +'.tex', 'w')
latex= res.summary().as_latex()
f.write(latex)
f.close()
or even better 甚至更好
for (res, name) in zip(result, filename):
with f = open(name +'.tex', 'w')
latex = res.summary().as_latex()
f.write(latex)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.