我需要将两个文件中的数据相互减去,但是我收到了这个错误:NameError:name' first1'没有定义。你能告诉我如何解决这个错误吗?我用来运行代码的命令在这里:python script.py input1.txt input2.txt> output.txt的
import numpy
from numpy import *
import scipy
from pylab import *
import sys
def main(argv):
t = open(sys.argv[1])
first1 = t.readline()
tt = open(argv[2])
second2 = tt.readline()
return [first1], [second2]
def result(first1, second2):
first = np.array(first1, dtype = np.float64)
second = np.array(second2, dtype = np.float64)
#subtract
sub = (first - second)
return [sub]
result(first1, second2)
if __name__ == '__main__':
sys.exit(main(sys.argv))
input1.txt:
2.5
2.8
3.9
4.2
5.8
input2.txt:
0.8
2.5
3.2
5.8
6.3
答案 0 :(得分:0)
我不确定你要做什么,但你的代码中有很多错误。这是一个工作版本:
import sys
def main(argv):
t = open(argv[1])
first1 = list(map(float, t.read().split('\n')))
tt = open(argv[2])
second2 = list(map(float, tt.read().split('\n')))
print(*result(first1, second2), sep = "\n")
def result(first1, second2):
return [round(first1[i] - second2[i], 3) for i in range(len(first1))]
if __name__ == '__main__':
main(sys.argv)