Python 我收到此错误-索引器:列表索引超出范围
我想加载Python 我收到此错误-索引器:列表索引超出范围,python,list,numpy,split,append,Python,List,Numpy,Split,Append,我想加载numpy文件以输入CNN,但我得到了这个索引器。我不知道为什么preictalspectrograms[cont].append(line.split(“”)[2].rstrip())索引器:列表索引超出范围 任何帮助都将不胜感激 while(line.strip()!=""): print('linex: ', line) print('\n') if("SEIZURE" in line):
numpy
文件以输入CNN,但我得到了这个索引器
。我不知道为什么preictalspectrograms[cont].append(line.split(“”)[2].rstrip())索引器:列表索引超出范围
任何帮助都将不胜感激
while(line.strip()!=""):
print('linex: ', line)
print('\n')
if("SEIZURE" in line):
line=f.readline()
if(len(line.split(' '))>=3):
preictalSpectograms.append([])
cont=cont+1
preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
indFilePathRead=indFilePathRead+1
else:
if(len(line.split(' '))>=3):
preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
indFilePathRead=indFilePathRead+1
由于您没有发布所有相关的代码,因此很难给您提供一个有保证的解决方案,但我的猜测是,您在执行第一个
append()
调用之前就在增加cont
,因此您实际上从未追加到列表中的第一项
while(line.strip()!=""):
print('linex: ', line)
print('\n')
if("SEIZURE" in line):
line=f.readline()
if(len(line.split(' '))>=3):
preictalSpectograms.append([])
preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
indFilePathRead=indFilePathRead+1
cont=cont+1
else:
if(len(line.split(' '))>=3):
preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
indFilePathRead=indFilePathRead+1
我的猜测是cont中出现的任何数字都大于preictalspectrograms的大小请提供一个包含两个列表索引操作的行,
preictalspectrograms[cont]
和line.split(“”)[2]
。你的工作是确定哪些是问题。前面的if
行应该可以防止第二行出现问题(但为什么要拆分两次?)。在这种情况下,cont
必须关闭。您是附加到前庭频谱图本身,还是附加到其中的列表?您需要逐行、逐次地测试代码块。不要猜测发生了什么事。测试,打印,测试,再打印!顺便问一下,你所说的numpy文件
是什么意思?它们是csv
格式文本吗?