python - 如何通过python运行一行文件并获取输出
问题描述
我想读取文件的一行并将其提供给 python 并获取输出。例如
我写:
a = 0
b = 10
c = 100
a == b-10 and b== c-100
在蟒蛇中:
file = open("new.txt")
run(file[0])
run(file[1])
run(file[2])
print(out(file[3]))
我想要最后一行的 True 。任何想法?
解决方案
最后一行,a == b - 10 和 b == c-100 由于 b != c-100 不评估为 True,但我认为您正在寻找的是:
with open("input.txt") as f:
lines = [line for line in f.readlines()]
exec(lines[0])
exec(lines[1])
exec(lines[2])
print(eval(lines[3]))
推荐阅读
- javascript - 如何在另一个函数中使用一个函数的变量而不是全局变量?
- openlayers - 是否可以在 OpenLayers 5 中定义视图的有效视口区域?
- powershell - PS Forms - 在 ForEach 循环中使用 BREAK 未处理的异常
- vmware-clarity - 更改标题颜色
- reactjs - 可以使用 React.Suspense 和 React.Lazy 转发 ref 吗?
- javascript - 用 Mustache 渲染空数组/对象
- three.js - 在相机对象上应用 THREE.DeviceOrientationControls 后的初始 Y 轴旋转
- android - 如何增加从 setSupportActionBar 创建的汉堡图标的大小
- opencl - 已完成事件的 OpenCL 回调
- python - Python iterrows() 从 Dataframe 索引和删除行