1

My program was working perfectly; then, it would give me an error that states the following:

Traceback (most recent call last):
  File "K:\56_CRT\1 PST EEIC\2_Projects\InternetOfTests\Project_Jacob\Full 
  Program (Testing).py", line 228, in <module>
    inputSamplesm[idx] = [data[0], data[1]]
IndexError: list index out of range

I don't know what that is. I've been trying to manipulate the arrays and indices, but I seem to fail to solve the problem. Please advise. Find my code below:

with open('MagnaDC Set Points.csv', 'r') as csvfile1, open('Amatek Set Points.csv', 'r') as csvfile2:
    dataset = csv.reader(csvfile1, csvfile2, 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])
        while time.time() < stopTime:
            s.sendall('MEAS:VOLT?\n'.encode('utf-8'))
            voltm = s.recv(1024)
            voltm = float(voltm)
7
  • IndexError: list index out of range means that the index you are trying to access does not exist. So, either inputSamplesm[idx], data[0] or data[1] does not exist. Commented Aug 8, 2018 at 7:42
  • 1
    Basic debugging would require to print the content of inputSamplesm and data and check whether your indexes exist. Honestly, no need to ask a question for this. Commented Aug 8, 2018 at 7:44
  • Yes I know but I'm still new to programming and I tried printing both inputSamplesm and data. What I get are values from the csv fie in inputSamplesm and the values written in indices 1 and 0 from data Commented Aug 8, 2018 at 7:51
  • Check if you can pass two csv file descriptors to reader function. In dataset = csv.reader(csvfile1, csvfile2, delimiter=',') content of csvfile1 may be empty or not delimited by , Commented Aug 8, 2018 at 7:51
  • 1
    Thanks @Jacob, I think the answer below could explain your problem! Commented Aug 8, 2018 at 8:02

1 Answer 1

3

As per documentation csv module's reader function accepts only one file descriptor to read contents. When you pass two file descriptors for the function, though the interpreter does not raise any exceptions it silently ignores the second and parses only the first file. You may need to revisit code by parsing one by one and also make sure that both files are non-empty.

Demonstration

file1.csv

name,age
swadhikar,29

file2.csv

name,age
elavarasan,29

sample script

import csv


with open('file2.csv') as f1, open('file1.csv') as f2:
    reader_obj = csv.reader(f1, f2, delimiter=',')
    next(reader_obj)
    for line in reader_obj:
        print(line)

Result

/usr/bin/python3.6 /home/swadhi/PycharmProjects/pyselenium/python/stackoverflow/so_51741056.py
['elavarasan', '29']

You may parse the two csv files with two separate reader references created using csv.reader() and zip them to iterate through them as below.

import csv


with open('file1.csv') as f1, open('file2.csv') as f2:
    f1_reader_obj = csv.reader(f1)
    f2_reader_obj = csv.reader(f2)

    next(f1_reader_obj)
    next(f2_reader_obj)

    for file1_line, file2_line in zip(f1_reader_obj, f2_reader_obj):
        line1_as_str = ' '.join(file1_line)
        line2_as_str = ' '.join(file2_line)
        print(line1_as_str)
        print(line2_as_str)

Result

/usr/bin/python3.6 /home/swadhi/PycharmProjects/pyselenium/python/stackoverflow/so_51741056.py
swadhikar 29
elavarasan 29
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. But can't I open and manipulate both files simultaneously?
@JacobAbiGerges Hi, I have updated the answer with approach to handle two csv files. Feel free to mark as "Best Answer" if you are satisfied with the answer! Good days

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.