同时读取几个.csv文件[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同时读取几个.csv文件[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我试图通过两个不同的.csv文件循环读取值。打开一个.csv文件时,我能够完美地运行程序;但是,当我导入第二个.csv文件时,我收到如下错误:
回溯(最近一次调用最后一次):文件“C: Users crteeic Desktop Project Full Program(Testing).py”,第210行,打开('MagnaDC Set Points.csv','r')和('Amatek Set Points.csv','r')为csvfile:
AttributeError:输入
该程序的目的是从两个.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}
'.format(data[0]).encode('utf-8'))
conn.write('VOLT {0}
'.format(data[0]).encode('utf-8'))
stopTime = testStartTime + int(data[1])
请指教。谢谢。
答案
您需要单独打开文件:
with open('MagnaDC Set Points.csv', 'r') as csvfile1, open('Amatek Set Points.csv', 'r') as csvfile2:
更多信息:Read two textfile line by line simultaneously -python
另一答案
如果您只想连续读取这两个文件并发送它们,请循环遍历文件名:
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}
".format(data[0]).encode("utf-8"))
conn.write("VOLT {0}
".format(data[0]).encode("utf-8"))
stopTime = testStartTime + int(data[1])
以上是关于同时读取几个.csv文件[重复]的主要内容,如果未能解决你的问题,请参考以下文章