简体   繁体   English

如何将python文件的输出存储到变量中

[英]How to store output from a python file into a variable

I m calling an executable in a python script which gives an output in the terminal as follows: 我在python脚本中调用可执行文件,该脚本在终端中提供如下输出:

Number of Iterations: 150 迭代数:150

Average time per step is 0.306834 ms 每步平均时间为0.306834毫秒

Average time for collisions is 0.027553 ms 平均碰撞时间为0.027553毫秒

Average time for velocity updates is 0.109900 ms 速度更新的平均时间为0.109900毫秒

Average time for position updates is 0.031053 ms 位置更新的平均时间为0.031053毫秒

Total loop time is 46.2161 ms 总循环时间为46.2161 ms

But i want to store this text in a variable so that i can edit it. 但是我想将此文本存储在变量中,以便我可以对其进行编辑。 How to do this??? 这个怎么做???

Use subprocess . 使用子过程 It lets you redirect the output. 它使您可以重定向输出。 You could simply do: 您可以简单地执行以下操作:

output = subprocess.check_output(args)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM