Why I get a list index error, if the code runs fine?
Question:
I have 7 txt data files, and I want to compute the standard deviation of the numbers in the second column, for each file.
I wrote this code:
from pathlib import Path
destdir = Path()
files = [p for p in destdir.iterdir() if p.is_file()]
for p in files:
with p.open() as f:
adatok=[]
for i in f:
adatok.append(float(i.split()[1]))
atlag=sum(adatok)/len(adatok)
elofordulas=[]
for j in adatok:
if j not in elofordulas:
elofordulas.append(j)
asd1=[]
for j in elofordulas:
asd1.append(adatok.count(j)*((atlag-j)**2))
szoras=sqrt(sum(asd1)/len(adatok))
print(p)
print(szoras)
print('-------')
It runs fine, but I get this error:
IndexError
Traceback (most recent call last)
in ()
6 adatok=[]
7 for i in f:
—-> 8 adatok.append(float(i.split()1))
9
10 atlag=sum(adatok)/len(adatok)
IndexError: list index out of range
Why? And what should I do?
Edit: This is a part of my data file, all lines are the same
Answers:
I bet you got a line without a space or a tab in one of your csv files. To be sure, just do some exception handling:
import sys
from math import sqrt
from pathlib import Path
if __name__ == '__main__':
destdir = Path()
files = [p for p in destdir.iterdir() if p.is_file()]
for p in files:
with p.open() as f:
adatok = []
for line_no, line in enumerate(f, start=1):
try:
adatok.append(float(line.split()[1]))
except Exception as e:
print(f"Exception '{e}' in line {line_no} of file '{p}' reading '{line.strip()}'")
sys.exit(-1)
atlag = sum(adatok) / len(adatok)
elofordulas = []
for j in adatok:
if j not in elofordulas:
elofordulas.append(j)
asd1 = []
for j in elofordulas:
asd1.append(adatok.count(j) * ((atlag - j) ** 2))
szoras = sqrt(sum(asd1) / len(adatok))
print(p)
print(szoras)
print('-------')
Update: We found that the stack trace was a bit misleading. The real mistake was that files
contained all files in the current folder. This, of course, included the Python script itself.
Lesson learned: When parsing multiple data file, proper exception handling is important.
With the proposed exception handling, the error would have been something like
Exception 'list index out of range' in line 1 of file 'script.python' reading 'import'
making it more obvious, where to look for the mistake.
I have 7 txt data files, and I want to compute the standard deviation of the numbers in the second column, for each file.
I wrote this code:
from pathlib import Path
destdir = Path()
files = [p for p in destdir.iterdir() if p.is_file()]
for p in files:
with p.open() as f:
adatok=[]
for i in f:
adatok.append(float(i.split()[1]))
atlag=sum(adatok)/len(adatok)
elofordulas=[]
for j in adatok:
if j not in elofordulas:
elofordulas.append(j)
asd1=[]
for j in elofordulas:
asd1.append(adatok.count(j)*((atlag-j)**2))
szoras=sqrt(sum(asd1)/len(adatok))
print(p)
print(szoras)
print('-------')
It runs fine, but I get this error:
IndexError
Traceback (most recent call last)
in ()
6 adatok=[]
7 for i in f:
—-> 8 adatok.append(float(i.split()1))
9
10 atlag=sum(adatok)/len(adatok)IndexError: list index out of range
Why? And what should I do?
Edit: This is a part of my data file, all lines are the same
I bet you got a line without a space or a tab in one of your csv files. To be sure, just do some exception handling:
import sys
from math import sqrt
from pathlib import Path
if __name__ == '__main__':
destdir = Path()
files = [p for p in destdir.iterdir() if p.is_file()]
for p in files:
with p.open() as f:
adatok = []
for line_no, line in enumerate(f, start=1):
try:
adatok.append(float(line.split()[1]))
except Exception as e:
print(f"Exception '{e}' in line {line_no} of file '{p}' reading '{line.strip()}'")
sys.exit(-1)
atlag = sum(adatok) / len(adatok)
elofordulas = []
for j in adatok:
if j not in elofordulas:
elofordulas.append(j)
asd1 = []
for j in elofordulas:
asd1.append(adatok.count(j) * ((atlag - j) ** 2))
szoras = sqrt(sum(asd1) / len(adatok))
print(p)
print(szoras)
print('-------')
Update: We found that the stack trace was a bit misleading. The real mistake was that files
contained all files in the current folder. This, of course, included the Python script itself.
Lesson learned: When parsing multiple data file, proper exception handling is important.
With the proposed exception handling, the error would have been something like
Exception 'list index out of range' in line 1 of file 'script.python' reading 'import'
making it more obvious, where to look for the mistake.