0

I am trying to read values through a loop from two different .csv files. I am being able to run the program perfectly when opening one .csv file; however, when I import the second .csv file, I get an error stated below:


Traceback (most recent call last): File "C:\Users\crteeic\Desktop\Project\Full Program (Testing).py", line 210, in with open('MagnaDC Set Points.csv', 'r') and ('Amatek Set Points.csv', 'r') as csvfile:

AttributeError: enter

The aim of the program is to read values from two .csv files and send these values to two different power supplies. Please find the code below:

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])

Please advice. Thank you.

2 Answers2

2

You need to open files separately:

with open('MagnaDC Set Points.csv', 'r') as csvfile1, open('Amatek Set Points.csv', 'r') as csvfile2: 

More info: Read two textfile line by line simultaneously -python

Adelina
  • 10,915
  • 1
  • 38
  • 46
2

If you want to just serially read both files and send them, loop over the filenames:

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])
AKX
  • 152,115
  • 15
  • 115
  • 172