Saving values in an array using loops

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Saving values in an array using loops



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


Traceback (most recent call last):
File "K:56_CRT1 PST EEIC2_ProjectsInternetOfTestsProject_JacobFull
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 0n'.format(data[0]).encode('utf-8'))
conn.write('VOLT 0n'.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)





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.
– Windmill
Aug 8 at 7:42


IndexError: list index out of range


inputSamplesm[idx]


data[0]


data[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.
– toti08
Aug 8 at 7:44


inputSamplesm


data





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
– Jacob Abi Gerges
Aug 8 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 ,
– Swadhikar C
Aug 8 at 7:51


reader


dataset = csv.reader(csvfile1, csvfile2, delimiter=',')


,





Thanks @Jacob, I think the answer below could explain your problem!
– toti08
Aug 8 at 8:02




1 Answer
1



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.


csv


reader



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.


csv.reader()


zip


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





Thanks for the help. But can't I open and manipulate both files simultaneously?
– Jacob Abi Gerges
Aug 8 at 8:07





@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
– Swadhikar C
Aug 8 at 8:44






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered