我正在尝试通过循环从两个不同的.csv文件读取值。打开一个.csv文件时,我能够完美运行该程序;但是,当我导入第二个.csv文件时,出现以下错误:
回溯(最近通话最近): 文件“ C:\ Users \ crteeic \ Desktop \ Project \ Full Program(Testing).py”,第210行,在 将open('MagnaDC Set Points.csv','r')和('Amatek Set Points.csv','r')打开为csvfile:
该程序的目的是从两个.csv文件读取值并将这些值发送到两个不同的电源。请在下面找到代码:
with open('MagnaDC Set Points.csv', 'r') and ('Amatek Set Points.csv', 'r') as csvfile:
dataset = csv.reader(csvfile, delimiter=',')
next(dataset)
rows = list(dataset)
inputSamplesm = np.empty([len(rows), 2], dtype=float)
outputSamplesm = np.empty([1,3], dtype=float)
inputSamplesa = np.empty([len(rows), 2], dtype=float)
outputSamplesa = np.empty([1,3], dtype=float)
testStartTime = time.time()
for idx, data in enumerate(rows):
inputSamplesm[idx] = [data[0], data[1]]
inputSamplesa[idx] = [data[0], data[1]]
s.sendall('VOLT {0}\n'.format(data[0]).encode('utf-8'))
conn.write('VOLT {0}\n'.format(data[0]).encode('utf-8'))
stopTime = testStartTime + int(data[1])
请咨询。谢谢。
答案 0 :(得分:2)
您需要单独打开文件:
with open('MagnaDC Set Points.csv', 'r') as csvfile1, open('Amatek Set Points.csv', 'r') as csvfile2:
答案 1 :(得分:2)
如果您只想串行读取两个文件并发送它们,请遍历文件名:
filenames = [
"MagnaDC Set Points.csv",
"Amatek Set Points.csv",
]
for filename in filenames:
with open(filename, "r") as csvfile:
dataset = csv.reader(csvfile, delimiter=",")
next(dataset)
rows = list(dataset)
inputSamplesm = np.empty([len(rows), 2], dtype=float)
outputSamplesm = np.empty([1, 3], dtype=float)
inputSamplesa = np.empty([len(rows), 2], dtype=float)
outputSamplesa = np.empty([1, 3], dtype=float)
testStartTime = time.time()
for idx, data in enumerate(rows):
inputSamplesm[idx] = [data[0], data[1]]
inputSamplesa[idx] = [data[0], data[1]]
s.sendall("VOLT {0}\n".format(data[0]).encode("utf-8"))
conn.write("VOLT {0}\n".format(data[0]).encode("utf-8"))
stopTime = testStartTime + int(data[1])